如何在Python的while循环中修复值错误。

huangapple go评论69阅读模式
英文:

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 = &quot;\nWhat is your age?&quot;
prompt += &quot;\n(Please enter &#39;quit&#39; when you are finished.) &quot;


while True:
    age = input(prompt)
    age = int(age)

    if age &lt; 3:
        print(&quot;Your ticket is free.&quot;)
    elif age &gt;= 3 and age &lt;= 12:
        print(&quot;Your ticket costs $10.&quot;)    
    elif age &gt; 12:
        print(&quot;Your ticket costs $15.&quot;)  

    elif age == &#39;quit&#39;:
        break  

> It displays this when I run it:
>
>
&gt; What is your age?
&gt; (Please enter &#39;quit&#39; when you are finished.) 7
&gt; Your ticket costs $10.
&gt;
&gt; What is your age?
&gt; (Please enter &#39;quit&#39; when you are finished.) 8
&gt; Your ticket costs $10.
&gt;
&gt; What is your age?
&gt; (Please enter &#39;quit&#39; when you are finished.) 1
&gt; Your ticket is free.
&gt;
&gt; What is your age?
&gt; (Please enter &#39;quit&#39; when you are finished.) quit
&gt; Traceback (most recent call last):
&gt; File &quot;C:\Users\ibrah\OneDrive\Desktop\python_work\chapter_7\movie_tickets.py&quot;, line 7, in &lt;module&gt;
&gt; age = int(age)
&gt; ^^^^^^^^
&gt; ValueError: invalid literal for int() with base 10: &#39;quit&#39;
&gt;

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 &quot;quit&quot; to an int, which is raising the ValueError. Instead, you can try checking if age is &quot;quit&quot; before the int call:

prompt = &quot;\nWhat is your age?&quot;
prompt += &quot;\n(Please enter &#39;quit&#39; when you are finished.) &quot;


while True:
    age = input(prompt)  # Get input

    if age == &quot;quit&quot;:    # Check if &quot;quit&quot; was input
        break

    age = int(age)       # If not, convert to int

    if age &lt; 3:          # ...
        print(&quot;Your ticket is free.&quot;)
    elif age &gt;= 3 and age &lt;= 12:
        print(&quot;Your ticket costs $10.&quot;)    
    elif age &gt; 12:
        print(&quot;Your ticket costs $15.&quot;)  

You can also change your second elif to &lt;= 12 (we already know it's &gt;= 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

huangapple
  • 本文由 发表于 2023年7月20日 20:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定