Python: problem with raw_input with reading number

Unfortunately, raw_input does not do what I need for this. What I'm trying to do is get totPrimes = everything I type in the tooltip. If I replace while count < totPrimes with while count < 50 , this works the script. If I type 50 at the prompt, this script does not work, I'm afraid that raw_input is not the function I'm 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 : 
+6
source share
6 answers

Do

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

raw_input gives you a string that you must convert to an integer or float before doing any numerical comparison.

+11
source

You need to impose totPrimes on int as follows:

 integer = int(totPrimes) 
0
source

You just need to convert the original input to an integer. For your code, just change your code as:

 testNum = 3 div = 2 count = 1 totPrimes = raw_input("Please enter the primes: ") totPrimes=int(totPrimes) while count < totPrimes : while div <= testNum : 
0
source

Use input then.

Raw input returns a string.

input returns int.

0
source

The raw_input function always returns the type 'string' raw_input docs , so in this case, we must convert the string totPrimes enter the type 'int' or 'float' as follows:

 totPrimes = raw_input("Please enter the primes: ") totPrimes = float(totPrimes) 

You can combine them as follows:

 totPrimes = float(raw_input("Please enter the primes: ")) 

To compare things in python count < totPrimes , a comparison should make sense (numbers for numbers, strings for strings) or the program will work, and the while count < totPrimes : while loop will not while count < totPrimes : .

You can use try / except to protect your program. exception management

For people taking the Programming for All course, you can take a few hours and evaluate this path. an if / else statement you should try to figure out.

0
source

You must change each number to "hrs" or "rate".

For example: 40*10.50+(h-40)*10.50*1.5 is incorrect, 40*r+(h-40)*r*1.5 right.

0
source

Source: https://habr.com/ru/post/886523/


All Articles