告诉我如何调用我的函数
Show me the way to calling my function
所以我正在学习编码,我从 Python 开始。
我在 python 中学习了编程的基础知识,例如什么是变量、运算符、一些函数等
我有这个代码:
def guessGame():
import random
guesses = 0
randomNo = random.randint(0, 100)
print "I think of a number between 0 and 100, you have 10 guesses to get it right!"
while guesses < 10:
guess = input("Take a guess ")
guess = int(guess)
guesses += 1
if guess < randomNo:
print "The number is higher than your guess"
if guess > randomNo:
print "The number is lower than your guess"
if guess == randomNo:
break
if guess == randomNo:
guesses = str(guesses)
print "You got it in %s guesses!" % guesses
if guess != randomNo:
print "You failed to guess!"
guessGame()
当我 运行 cmd 中的代码时,它在函数获取 "recalled" 之前结束。
cmd output
您在主程序中仅调用了一次游戏 -- 仅包含最后一行。它运行一次游戏然后退出。您的代码中没有第二次调用。也许你想要一个经典的 "play again?" 循环:
play = True
while play:
guessGame()
play = lower(raw_input("Play again?")[0]) == 'y'
每场比赛结束后,您都会询问玩家的意见。如果该输入以字母 'y'(大写或小写)开头,那么您将继续游戏;否则,play 变为 False,您退出循环。
这是你想要的吗?
所以我正在学习编码,我从 Python 开始。 我在 python 中学习了编程的基础知识,例如什么是变量、运算符、一些函数等
我有这个代码:
def guessGame():
import random
guesses = 0
randomNo = random.randint(0, 100)
print "I think of a number between 0 and 100, you have 10 guesses to get it right!"
while guesses < 10:
guess = input("Take a guess ")
guess = int(guess)
guesses += 1
if guess < randomNo:
print "The number is higher than your guess"
if guess > randomNo:
print "The number is lower than your guess"
if guess == randomNo:
break
if guess == randomNo:
guesses = str(guesses)
print "You got it in %s guesses!" % guesses
if guess != randomNo:
print "You failed to guess!"
guessGame()
当我 运行 cmd 中的代码时,它在函数获取 "recalled" 之前结束。 cmd output
您在主程序中仅调用了一次游戏 -- 仅包含最后一行。它运行一次游戏然后退出。您的代码中没有第二次调用。也许你想要一个经典的 "play again?" 循环:
play = True
while play:
guessGame()
play = lower(raw_input("Play again?")[0]) == 'y'
每场比赛结束后,您都会询问玩家的意见。如果该输入以字母 'y'(大写或小写)开头,那么您将继续游戏;否则,play 变为 False,您退出循环。
这是你想要的吗?