这是我的代码:
import random
import time
def gameintronew():
print('Hello! What\'s your name?')
myName = input()
time.sleep(2)
print('Well '+myName+', I am thinking of a number between 1 and 20.')
number = random.randint(1, 20)
guessesTaken = 0
guessesLeft = 6
gamestart()
def gameintrolold():
print('Okay '+myName+', I am thinking of another number between 1 and 20. Can you guess this one?')
number = random.randint(1, 20)
guessesTaken = 0
guessesLeft = 6
gamestart()
def gamestart():
while guessesTaken < 6:
if guessesTaken == 0:
print('Take a guess.')
else:
print('Take another guess.')
guess = input()
guess = int(guess)
if guess < number:
print('Your guess was too low.')
if guess > number:
print('Your guess was too high.')
if guess == number:
break
if guess == number:
print('Well done '+myName+'! The number I was thinking of was indeed '+str(number)+'! You guessed my number in '+str(guessesTaken)+' with '+str(guessesLeft)+' guesses left!')
playAgain()
if guess != number:
print('Nope. The number I was thinking of was '+str(number)+'.')
然而,当执行该操作时,没有响应,只是一个空行。我已经尝试过没有def,它工作正常,并尝试了不同的python平台,结果仍然相同。
非常感谢,
汤姆
答案 0 :(得分:3)
def
本身并不执行任何代码。它只需要一段代码并为其命名。
您必须调用函数才能输出任何内容,您必须在&#34;顶级&#34; (在任何类或模块定义之外)。