Python 2.7.7 破解?

Python 2.7.7 Breaking?

我正在尝试在 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!"

我需要帮助的事情是: 如果用户猜对了删除或替换的单词,则应停止所有其他猜测。如果删除和替换词猜对了我需要,打印 "You win" 我需要使用 break 语句吗?谢谢

我希望您代码中的缩进问题只是一个 copy/paste 错误,否则程序根本不会 运行ning。如果您的代码 运行s 在循环中(如 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.

这是一种方法:

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"