即使if条件为deck()
,if语句也不会在True
函数中执行。
我总是在问输入变量中输入字母,该变量总是退出in
机密或random_word,例如,如果random_word
是“ AAF”,则我会传递问值“ A”还是“ F“
import random
random_word = random.choice(open('sowpods.txt','r').read().split())
secret = [x for x in random_word]
print('secret: ', secret)
place = [' ' for x in range(len(random_word))]
print('place: ', place)
guessed = list()
chance = 0
play = True
print("Welcome to Hangman Game!")
print(random_word)
def board():
ask = input("Please guess the letter: ").upper
if ask in secret:
return("ask in secret")
else:
print('ask not in secret')
board()
输出:
secret: ['P', 'O', 'U', 'S', 'S', 'I', 'E']
place: [' ', ' ', ' ', ' ', ' ', ' ', ' ']
Welcome to Hangman Game!
POUSSIE
Please guess the letter: P
ask not in secret
我要执行if语句而不是else语句
答案 0 :(得分:3)
输入后打印出ask
(打印出或检查异常的变量通常应该是您的第一步调试步骤):
ask = input("Please guess the letter: ").upper
print(ask) # add this
如果这样做,您应该立即看到问题:
<built-in method upper of str object at 0x7f977c1da3e8>
^^^^^^^^^^^^
这是一个函数对象,而不是预期的字符串,这是因为您缺少了 call (而不是仅引用){{ 1}}函数。应该是:
upper
答案 1 :(得分:0)
.upper方法需要使用方括号和print(“秘密询问”)而不是return(“秘密询问”)
random_word = random.choice(open('sowpods.txt','r').read().split())
secret = [x for x in random_word]
print('secret: ', secret)
place = [' ' for x in range(len(random_word))]
print('place: ', place)
guessed = list()
chance = 0
play = True
print("Welcome to Hangman Game!")
print(random_word)
def board():
ask = input("Please guess the letter: ").upper()
if ask in secret:
print("ask in secret")
else:
print('ask not in secret')
board()