Python Hangman问题

时间:2015-10-07 17:07:57

标签: python

我正在尝试制作一个刽子手游戏。到目前为止,我有一个单词列表供该程序随机选择:

a = random.choice(hangman_words))

然后我让程序使用len功能显示单词中的字符数。这一切都应该是这样的。我遇到的问题是在提示用户输入信件之后,我需要程序告诉用户这封信是否正确,我不知道该怎么做。 感谢

2 个答案:

答案 0 :(得分:1)

也许尝试if语句?所以例如

if userAnswer == theRandomLetter then
  print "Correct!"
else:
  print "Try again!"

之后,如果答案不正确,您可以使用循环重复该过程

答案 1 :(得分:0)

以下是一些测试您病情的代码。

answer = raw_input("enter a letter:")

incorrect_letters = ["a","y","Y","q","Q","p"] 


for x in incorrect_letters:
    if answer == x:
        print "incorrect choice"

以下是一些生成字符的代码:

char_list = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
            "n","o","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","I","J","k","L","M",
            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

num_char= 100

my_char = [random.choice(char_list) for i in xrange(num_char)]