Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Problem with raw_input reading a number

unfortunately raw_input is not doing what I need it to do. What I am trying to do is get totPrimes = whatever I type in at the prompt. If i replace while count < totPrimes with while count < 50 this script works. If I type 50 into the prompt, this script doesnt work, I'm afraid raw_input isn't the function im looking to use? Here is a snippet of my code:

testNum = 3
div = 2
count = 1
totPrimes = raw_input("Please enter the primes: ")

while count < totPrimes :
    while div <= testNum :
like image 712
yoshyosh Avatar asked Apr 23 '11 07:04

yoshyosh


1 Answers

Do

totPrimes = int(totPrimes)
while count < totPrimes:
    # code

raw_input gives you a string you must convert to an integer or float before making any numeric comparison.

like image 96
joaquin Avatar answered Sep 20 '22 04:09

joaquin