gpt4 book ai didi

python - 如果python中的语句不打印

转载 作者:太空狗 更新时间:2023-10-30 02:43:15 25 4
gpt4 key购买 nike

我正在为学校做作业,这个程序 super 简单,但我的 if 语句不能正常工作。有人知道为什么吗?这是代码:

letter = input("Name one of the most common letters in wheel of fortune puzzles: ")

if letter == "r":
print(letter, "is one of the most common letters!")
if letter == "s":
print(letter, "is one of the most common letters!")
if letter == "t":
print(letter, "is one of the most common letters!")
if letter == "l":
print(letter, "is one of the most common letters!")
if letter == "n":
print(letter, "is one of the most common letters!")
if letter == "e":
print(letter, "is one of the most common letters!")

else:
print("Incorrect!")
letter = input("Name one of the most common letters in wheel of fortune puzzles: ")


input("\n\nPress the enter key to exit:")

输出是:

Name one of the most common letters in wheel of fortune puzzles: j
Incorrect!
Name one of the most common letters in wheel of fortune puzzles: r


Press the enter key to exit:

最佳答案

我会这样写:

# Assumes Python 3
# Python 2: Use `raw_input` instead of input

common_letters = 'rstlne'
prompt = "Name one of the most common letters in wheel of fortune puzzles: "

while True:
letter = input(prompt).strip()
if letter in common_letters:
print(letter, "is one of the most common letters!")
break
else:
print("Incorrect!")
answer = input('Type "end" to exit: ')
if answer.strip() == 'end':
break

我改变了一些东西:

  1. 我使用了 if letter in common_letters: 而不是那么多 if 语句。这使您可以向 common_letters 添加或删除另一个字母。

  2. 我使用 input(prompt).strip() 去除多余的空白。

  3. 如果没有输入常用字母,我会使用 while True 循环一遍又一遍地重复问题。 break 终止这个循环,即程序结束。

关于python - 如果python中的语句不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com