Python 2.7.7打破?

时间:2015-07-02 09:16:09

标签: python if-statement raw-input

我正在尝试在python 2.7.7中创建一个记忆游戏,我需要一些帮助我的代码。

Guess1_Easy_Removed = raw_input("Which Word Do You Think Was Removed?:")
if Guess1_Easy_Removed == wordList[9]:
    print "Correct!"
else:
    print "Try Again!"
Guess2_Easy_Removed = raw_input("Which Word Do You Think Was Removed?:")
if Guess2_Easy_Removed == wordList[9]:
    print "Correct!"
else:
    print "Try Again!"
Guess3_Easy_Removed = raw_input("Which Word Do You Think Was Removed?:")
if Guess3_Easy_Removed == wordList[9]:
    print "Correct!"
else:
    print "Try Again!"

Guess1_Easy_Substitute= raw_input("Which Word Do You Think Was The Substitute Word?:")
if Guess1_Easy_Substitute == wordList[5]:
    print "Correct!"    
else:
    print "Try Again!"

Guess2_Easy_Substitute= raw_input("Which Word Do You Think Was The Substitute Word?:")
if Guess2_Easy_Substitute == wordList[5]:
    print "Correct!"
else:
    print "Try Again!"
Guess3_Easy_Substitute= raw_input("Which Word Do You Think Was The Substitute Word?:")
if Guess3_Easy_Substitute == wordList[5]:
    print "Correct!"
else:
    print "Try Again!"

我需要帮助的是: 如果用户猜测删除或替换单词,则应停止所有其他猜测。如果删除&替换单词猜对了我需要,打印“你赢了”我需要使用break语句吗?感谢

2 个答案:

答案 0 :(得分:0)

我希望你的代码中的缩进问题只是一个复制/粘贴错误,否则程序根本就不会运行。如果您的代码在循环内运行(如for循环或while循环),那么您可以使用break语句退出循环。

循环示例 -

while True:
    Guess1_Easy_Removed = raw_input("Which Word Do You Think Was Removed?:")
    if Guess1_Easy_Removed == wordList[9]:
        print "Correct!"
        break    # <------- This would transfer the control out of the loop.
    else:
        print "Try Again!"
    ...... # you will need to do the break for all such scenarios.

如果您没有使用循环,那么您可以使用sys.exit(0)(0表示程序运行成功),为此您需要将sys模块导入程序,之后使用sys.exit()。这将导致程序结束。

示例 -

import sys
Guess1_Easy_Removed = raw_input("Which Word Do You Think Was Removed?:")
if Guess1_Easy_Removed == wordList[9]:
    print "Correct!"
    sys.exit(0)    # <------- This would cause the program to end.
else:
    print "Try Again!"
...... # you will need to do the break for all such scenarios.

答案 1 :(得分:0)

这是一种方法:

Guess1_Easy_Substitute= raw_input("Which Word Do You Think Was The Substitute Word?:")
while Guess1_Easy_Substitute != wordList[5]:
    Guess1_Easy_Substitute= raw_input("Try again! Which Word Do You Think Was The Substitute Word?:")
    if Guess1_Easy_Substitute == wordList[5]:
        print "You win"