Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在使用';而对';?_Python_Python 3.x - Fatal编程技术网

Python 为什么在使用';而对';?

Python 为什么在使用';而对';?,python,python-3.x,Python,Python 3.x,我试着写“y”和“n”,但我被困在这个不断循环中。我不明白为什么 为True时: 尝试: 答案=int(输入(“继续?是/否:”) 除值错误外: 打印(“请写y或n”) 持续 其他: 打破 如果答案==“y”: 打印(“是”) 如果答案='n': 打印(“否”) 正如注释中指出的-您需要的是字符而不是整数输入。将任何非数字转换为int将导致ValueError并重新开始 while True: answer = input("Continue? y/n: ").low

我试着写“y”和“n”,但我被困在这个不断循环中。我不明白为什么

为True时:
尝试:
答案=int(输入(“继续?是/否:”)
除值错误外:
打印(“请写y或n”)
持续
其他:
打破
如果答案==“y”:
打印(“是”)
如果答案='n':
打印(“否”)

正如注释中指出的-您需要的是字符而不是整数输入。将任何非数字转换为int将导致ValueError并重新开始

while True:
    answer = input("Continue? y/n: ").lower()
    if answer in ("y","n"):
        break
    print("Please write y or n.")

if answer == 'y': 
    print("Yes")
elif answer == 'n':
    print("No")

应该这样做。

int(input(“Continue?y/n:”)
这会尝试将您输入的任何内容转换为
int
。您认为
“y”
“n”
的整数版本是什么?请修复缩进简言之,删除
int()
。通常,它用于将
input()
的输出转换为
int
,以便对其进行一些数学运算。示例:
int(“1”)
返回
1
int(“y”)
会引发一个
ValueError
,因为没有等效的数值。