2017-01-09 88 views
-1

我有一个关于raw_input函数的问题。raw_input while循环后不工作

我作为一个小项目做了一个小数猜猜游戏,如果你不想玩它说“再见”。但是,我已将它作为raw_input,所以程序仅在按下后才会关闭输入

与此问题是,该程序完全查找raw_input位,而使用print("")代替工作。

我在哪里犯过一个错误?下面是代码:

import random 
from random import randint 
Number = 0 
Guess = 0 
while True: 
    Decision = raw_input("Do you want to play a round of number guess?(Yes/No)") 
    if Decision == "Yes": 
     Number = randint(0,100) 
     while True: 
      Guess = input("What number will you guess?") 
      if Guess == Number: 
       print("Correct!") 
       break 
      if Guess > Number: 
       print("Too big!") 
      if Guess < Number: 
       print("Too Small") 
    elif Decision == "No": 
     print("Oh well") 
     break 
    else: 
     print("I'm not what that means...") 
raw_input = ("Goodbye") 
+1

你在比较的'int'('Number')到'str'('Guess')。 ...并且在最后一行中,用''Goodbye''覆盖'raw_input'(这可能是您必须修复的第一件事)。 –

+3

'raw_input =(“Goodbye”)'用''Goodbye''替换'raw_input'函数。我想你只想'raw_input(“Goodbye”)' –

回答

0

考虑这条线从你的程序:

raw_input = ("Goodbye") 

此行不会调用raw_input()功能。相反,它将全局名称raw_input重新绑定到字符串"Goodbye"。由于这是您程序的最后一行,因此这条线没有实际效果。

如果你想在程序退出前暂停,试试这个:

raw_input("Goodbye") 
+0

不应该使用'input()'吗?它看起来像Python 3. –

+2

它不能是Python3,作者暗示它运行良好。否则,如果以前使用'raw_input()'和'input()'指示Python2。我不知道为什么作者使用功能符号'print()'。 –