why while loops does not stop? [duplicate] - python

This question already has answers here:
Python: Problem with raw_input reading a number [duplicate]
(6 answers)
Closed 5 years ago.
I am trying to print "count" , when count is smaller than my input value , but when I give the input value for X, it loos forever.
Can anybody tell me why ?
count = 0
x= raw_input()
while count <x :
print (count )
count +=1

By looking at the behaviour of the comparison operators (<, >, ==, !=), you can check that they treat integers as being smaller than non-empty strings. raw_input() returns a string (rather than an integer as you expected), hence your while loops indefinitely. Just switch to input():
count = 0
x = input()
while count < x:
print(count)
count += 1
Alternatively, you can use int(raw_input()), but I always use (and prefer) the former. All this is assuming you use Python 2.

Cast the input as an int so that the loop can increment it:
count = 0
x = int(raw_input())
while count <x :
print (count )
count +=1

Related

Python delete print [duplicate]

This question already has answers here:
Replace console output in Python
(12 answers)
Closed 11 months ago.
If I for example had a code like this:
score = 0
loop = true
while loop:
score = (score) + 1
print(score)
But I wanted to only print the new value of score instead of a long list of previous values aswell how would I do that?
print("Something", end='\r')
This way, any successive output will overwrite this one.
score = 0
while True:
score += 1
print(f"{score} ", end='\r')
Notice that in Python true is undefined, and unlike C, C++, JavaScript and some other languages, True and False are uppercase.

Python comparison if statement not working [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I am just messing around with Python trying to do a simple guessing program. The code inside my if statement never runs, and I can't figure out why. I've tried printing both variables at the end, and even when they're the same, the comparison never resolves to true. I've also tried just setting y as 2 and guessing 2 as the input, and it still doesn't work.
import random
x = input("Guess a number 1 or 2: ")
y = random.randint(1,2)
if x==y:
print("yes")
The problem here is that x is a string and y is an int:
x = input("Try a number ") # I chose 4 here
x
'4'
x == 4
False
int(x) == 4
True
input will always return a string, which you can convert to int using the int() literal function to convert that string into the required value

While loop over numbers doesn't seem to be working [duplicate]

This question already has answers here:
Reading in integer from stdin in Python
(4 answers)
Closed 4 years ago.
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers = []
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers = []
loop = int(loop)
...

Why does my Fibonacci series genetares an infinte loop? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
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 :
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.
You need to cast totPrimes into an int like this:
integer = int(totPrimes)
You just need to convert your raw input in a 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 :
The raw_input function always returns a 'string' type raw_input docs, so we must in this case convert the totPrimes 'string' type to an 'int' or 'float' type like this:
totPrimes = raw_input("Please enter the primes: ")
totPrimes = float(totPrimes)
You can combine them like this:
totPrimes = float(raw_input("Please enter the primes: "))
In order to compare things in python count < totPrimes, the comparison needs to make sense (numbers to numbers, strings to strings) or the program will crash and the while count < totPrimes : while loop won't run.
You can use try/except to protect your program. managing exceptions
For people taking the course "Programming for Everybody" you can take in hours and rate this way. the if/else statement you should try to figure out.
You have to change every number to "hrs" or "rate".
For example: 40*10.50+(h-40)*10.50*1.5 is wrong, 40*r+(h-40)*r*1.5 is right.
Use input then.
Raw input returns string.
input returns int.

Python passing arguments from console [duplicate]

This question already has answers here:
python: while loop not checking the condition [closed]
(3 answers)
Closed 8 years ago.
In the following code:
def foo(n):
print "n value before if:",n #displays given num
if n <= 2:
print "n value:",n #not displayed even n is less than 2
num = raw_input()
print foo(num)
The if statement does not execute on giving inputs less than 2 for num.
So, why is if statement not executing?
raw_input returns a string, you are then comparing it to an integer.
Try converting it to an int:
num = int(raw_input())

Categories