【问题标题】:If statement in python not printing如果python中的语句不打印
【发布时间】:2015-12-08 04:53:49
【问题描述】:

我正在为学校做作业,这个程序非常简单,但是我的 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:

【问题讨论】:

  • 您需要更具体。 “不工作”是什么意思?提供样本输入和输出。此外,您的else 仅连接到最后一个if,因此如果字母不是“e”,您肯定会看到"Incorrect!"。请改用elif ... :
  • 好的,抱歉。输出是
  • 我只是编辑我的原件

标签: python python-3.x if-statement


【解决方案1】:

我会这样写:

# 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 终止此循环,即程序结束。

【讨论】:

  • 谢谢!这行得通。其他人认为我正在运行 python2,但我正在使用 python3。
  • 现在我将如何让它结束循环,并且在它第一次询问“说出命运之轮谜题中最常见的字母之一:”它不会打印“不正确!” ,我该怎么做呢?
  • @E.Ingraham 你运行程序了吗?当字母很常见时,它会提出这个问题并退出。否则会打印“不正确!”并不断询问。
  • 我复制不正确,对不起!现在可以了。非常感谢您的帮助!
  • 添加了另一个问题,询问用户是否想结束程序。
【解决方案2】:

您需要输入"r" 或简单使用raw_input().raw_input 接受stringinput=eval(raw_input(prompt)).不是这种情况@所以它会搜索r 变量,除非你说@ 987654327@ 告诉它它是一个字符串。

【讨论】:

  • 对不起....我对这个东西真的很陌生,你能用更“愚蠢”的术语解释一下吗?我的B
  • 这样吗? 'letter = input("说出命运之轮谜题中最常见的字母之一:") if letter == r: print(letter, "是最常见的字母之一!") elif letter == s: print(字母,“是最常见的字母之一!”)'等......
  • 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!") elif letter == s: print(letter, "is one of the most common letters!")
  • 好的,我怎么让我给r?这是作业的要求。
  • 我试过了,没有任何区别。 raw_input() 函数没有像通常的函数那样着色。
【解决方案3】:

您可以在您的代码中进行以下更改。

  1. 从第二个if 条件将其更改为elif,因为如果您输入例如's',那么它将进入第二个if 条件,并且else 部分也将运行。

  2. input更改为raw_input示例更改

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

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

【讨论】:

  • 我做了你的第 1 步,但 raw_input 给了我一个错误raw_input is not defined
  • raw_input 应该默认工作。类似于input,唯一不同的是不用''输入任何内容
  • 它在 Python 2 中定义并有用,但问题显然与 Python 3 有关。
  • 好的。没有看问题中的 Python 3 标签
【解决方案4】:
#considering you enter 'j' and 'r' here's the path your code takes

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

if letter == "r": # letter don't equal 'r', don't do next line
    print(letter, "is one of the most common letters!")
if letter == "s": # letter don't equal 's', don't do next line
    print(letter, "is one of the most common letters!")
if letter == "t": # letter don't equal 't', don't do next line
    print(letter, "is one of the most common letters!")
if letter == "l": # letter don't equal 'l', don't do next line
    print(letter, "is one of the most common letters!")
if letter == "n": # letter don't equal 'n', don't do next line
    print(letter, "is one of the most common letters!")
if letter == "e": # letter don't equal 'e', don't do next line
    print(letter, "is one of the most common letters!")

else: # because last if was false (letter don't equal 'e') do next line.
    print("Incorrect!") #  output : Incorrect!
# next line is not consider inside else. should be included within {} if desired
    letter = input("Name one of the most common letters in wheel of fortune puzzles: ") 
# letter now equals 'r'

#nothing to do with it


input("\n\nPress the enter key to exit:") 
# showing Press the enter key to exit: and waiting.

这会按照我的想法工作。

letter = raw_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!")
elif letter == "s":
    print(letter, "is one of the most common letters!")
elif letter == "t":
    print(letter, "is one of the most common letters!")
elif letter == "l":
    print(letter, "is one of the most common letters!")
elif letter == "n":
    print(letter, "is one of the most common letters!")
elif letter == "e":
    print(letter, "is one of the most common letters!")
else:
    print("Incorrect!")

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

【讨论】:

  • 请在发布之前测试您的代码。 Python 中的注释以# 开头。
最近更新 更多