英文:
How to fix Value error in a while loop in python
问题
我写了一个带有while循环语句的小程序。所有行都运行得很好,除了代码的最后一行,应该退出循环。每当Python尝试解释它时,它总是显示错误。我已经尝试以多种方式重新构造代码,但都没有成功。你能帮帮我吗?
这是程序:
prompt = "\nWhat is your age?"
prompt += "\n(Please enter 'quit' when you are finished.) "
while True:
age = input(prompt)
if age == 'quit':
break
age = int(age)
if age < 3:
print("Your ticket is free.")
elif age >= 3 and age <= 12:
print("Your ticket costs $10.")
elif age > 12:
print("Your ticket costs $15.")
当我运行它时,它显示如下:
What is your age? (Please enter 'quit' when you are finished.) 7 Your ticket costs $10. What is your age? (Please enter 'quit' when you are finished.) 8 Your ticket costs $10. What is your age? (Please enter 'quit' when you are finished.) 1 Your ticket is free. What is your age? (Please enter 'quit' when you are finished.) quit
我尝试将引发错误的行的值转换为字符串,但仍然不起作用。
英文:
I wrote a little program with a while loop statement. All lines run perfectly fine except for the last line of code which should break the loop. It always displays an error whenever Python tries to interpret it. I have tried changing rephrasing the code in multiple ways but to no avail. Could you please help me out?
This is the program:
prompt = "\nWhat is your age?"
prompt += "\n(Please enter 'quit' when you are finished.) "
while True:
age = input(prompt)
age = int(age)
if age < 3:
print("Your ticket is free.")
elif age >= 3 and age <= 12:
print("Your ticket costs $10.")
elif age > 12:
print("Your ticket costs $15.")
elif age == 'quit':
break
> It displays this when I run it:
>
>
> What is your age?
> (Please enter 'quit' when you are finished.) 7
> Your ticket costs $10.
>
> What is your age?
> (Please enter 'quit' when you are finished.) 8
> Your ticket costs $10.
>
> What is your age?
> (Please enter 'quit' when you are finished.) 1
> Your ticket is free.
>
> What is your age?
> (Please enter 'quit' when you are finished.) quit
> Traceback (most recent call last):
> File "C:\Users\ibrah\OneDrive\Desktop\python_work\chapter_7\movie_tickets.py", line 7, in <module>
> age = int(age)
> ^^^^^^^^
> ValueError: invalid literal for int() with base 10: 'quit'
>
I tried converting that value in the line which brings up the error to a string, but it still didn't work.
答案1
得分: 3
你试图将"quit"
转换为int
,这会引发ValueError
。相反,您可以尝试在int
调用之前检查age
是否为"quit"
:
prompt = "\nWhat is your age?"
prompt += "\n(Please enter 'quit' when you are finished.) "
while True:
age = input(prompt) # 获取输入
if age == "quit": # 检查是否输入了"quit"
break
age = int(age) # 如果不是,转换为int
if age < 3: # ...
print("Your ticket is free.")
elif age >= 3 and age <= 12:
print("Your ticket costs $10.")
elif age > 12:
print("Your ticket costs $15.")
您还可以将第二个elif
更改为<= 12
(因为我们已经知道它是>= 3
),将最后一个elif
更改为else
。
英文:
You're trying to convert "quit"
to an int
, which is raising the ValueError
. Instead, you can try checking if age
is "quit"
before the int
call:
prompt = "\nWhat is your age?"
prompt += "\n(Please enter 'quit' when you are finished.) "
while True:
age = input(prompt) # Get input
if age == "quit": # Check if "quit" was input
break
age = int(age) # If not, convert to int
if age < 3: # ...
print("Your ticket is free.")
elif age >= 3 and age <= 12:
print("Your ticket costs $10.")
elif age > 12:
print("Your ticket costs $15.")
You can also change your second elif
to <= 12
(we already know it's >= 3
) and your last elif
to an else
.
答案2
得分: 0
你必须检查你的 'prompt' 变量是否为整数。
使用以下代码进行检查:
if ((type(prompt)==int) || (prompt.is_integer())):
//执行定价工作
else:
//检查是否输入了退出
英文:
You must check if your 'prompt' variable is integer or not.
Use the following code to check so
if ((type(prompt)==int) || (prompt.is_integer())):
//do your pricing staff
else:
//check if quit entered
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论