Python-raw_input 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16003682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:34:14  来源:igfitidea点击:

Python- raw_input not working

pythondefined

提问by user2278741

Ok so I changed it to:

好的,所以我将其更改为:

if input('a'):
      print ("You: Gimme a gun!")

if input('b'):
       print ("You: Fine")

But now I don't get a choice it forces me to choose a and then after that it forces me to choose b, once I get past this hurdle I have the rest of the game in the bag but I really need help to figure this out P.S. I am noob to python

但现在我没有选择,它迫使我选择 a,然后它迫使我选择 b,一旦我克服了这个障碍,我就可以完成剩下的比赛,但我真的需要帮助来解决这个问题PS我是python的菜鸟

import time
Gimme=True
Fine=True




print ("James: Ah, it looks like subject 091-266 is awake")
time.sleep(4)
print ("Scarlet: Hello, do you remember anything? The crash or anything?")
time.sleep(4)
print ("You: What.... Where am I?")
time.sleep(3)
print ("Scarlet: Oh, where are my manners, this is the head quarters of the XionRepublic, Xion")                        
time.sleep(5)
print ("James: You were involved in Z-9102, code named Attack-Z")
time.sleep(4)
print ("Scarlet: We were able to pull you and three others out before we  were forced to...")                                             
print ("James: Exterminate Alpha Base 12.")
time.sleep(6)
print ("You: Exterminate?! Couldn't you just quarantine it?")
time.sleep(4)
print ("Scarlet: No, Alpha Base 12 had over 3,000 people in it, it was to risky to quarantine")      
time.sleep(5)
print ("James: Do you recognize these names, Noah, Alex or Robert?")
time.sleep(4)
print ("You: Yes Alex!? Why? Is he ok?!")
time.sleep(3)
print ("James: Yes, Yes he was one of the three.")
time.sleep(4)
print ("*ALARM! SECURITY BREACHED, SECURITY BREACHED*")
time.sleep(4)
print ("James: Scarlet lock the door!")
time.sleep(3)
print ("You: Whats going on?!")
time.sleep(3)
print ("James: Z's there here.")
time.sleep(3)
print ("*Screaming*")
time.sleep(2)
print ("You: I can fight!")
time.sleep(3)
print ("Scarlet: Trust me you are not in any condition to fight due to some of the drugs in you")   
print ("CHOICE")
print ("A.Gimme the gun!")
print ("B.Fine")

if raw_input() == Gimme:
    print ("You: Gimme a gun!")
if raw_input() == Fine:
    print ("You: Fine")

采纳答案by elder4222

If you are using the new versions of python -- 3.x.x -- then raw_input no longer exists. Use input(prompt) instead. It works pretty much the same. basic Syntax:

如果您使用的是新版本的 python -- 3.xx -- 那么 raw_input 不再存在。改用输入(提示)。它的工作原理几乎相同。基本语法:

foo = input("some prompt"). 

What input does is it reads a line from the standard input file, or <stdin>. It prints the prompt within the ()and then waits for user input. Example: (>>>is the command line prompt, <<<is output

input 的作用是从标准输入文件中读取一行,或者<stdin>. 它在 中打印提示(),然后等待用户输入。示例:(>>>是命令行提示符,<<<是输出

Command Line, interactive mode (or IDLE): 
>>> foo = input("GIMME SOME INPUT: ")  #tell it to take some input
<<<GIMME SOME INPUT: foo          # it prints out, "GIMME SOME INPUT:" user types in foo
>>> print(foo)
<<< foo

Response to your edit:

对您的编辑的回应:

Use this:

用这个:

print ("CHOICE")
print ("A.Gimme the gun!")
print ("B.Fine")
choice = input("What do you choose?")
if choice == 'A' or choice == 'a':
    #Some Action 
if choice == 'B' or choice == 'b': 
    #Some Other Action  

回答by Tim Pietzcker

You appear to be using Python 3. In Python 3, raw_input()has been renamed to input().

您似乎在使用 Python 3。在 Python 3 中,raw_input()已重命名为input().

回答by Eevee

Re your new question:

回复你的新问题:

But now I don't get a choice it forces me to choose a and then after that it forces me to choose b

但现在我没有选择它迫使我选择 a 然后它迫使我选择 b

That's because you're calling input()twice, and each time you call it, you're prompted to type something. You want to call it once, store the value you get in a variable, and compare that variable to the possible choices.

那是因为您调用了input()两次,每次调用它时,都会提示您输入一些内容。您想调用它一次,将获得的值存储在一个变量中,然后将该变量与可能的选择进行比较。