Reputation: 69
Why is my while loop infinite when using bob and (5 for example)? New to python so if this is too basic of a question.
from splashkit import *
def read_string (prompt):
write_line(prompt)
result = read_line()
return result
def read_interger (prompt):
line = read_string(prompt)
while not line.isdigit():
print("Please enter a whole number")
line = read_string(prompt)
return int(line)
name = read_string("Enter your name: ")
age = read_interger("Enter your age: ")
print("Hello", name)
print("Aged: ", age)
Upvotes: 1
Views: 296
Reputation: 114481
A wild guess... (the documentation of read_line()
doesn't say much about what the function does).
The problem is that the line read also contains the newline '\n'
character and therefore .isdigit()
is going to be always false because, as the documentation says, the string must be not empty and ALL the characters in the string must be digits.
You can chage the input to result = read_line().strip()
to solve this problem as it will remove the ending newline character.
Upvotes: 1