本文主要是介绍猜数字的游戏Python3,用Python自动化无聊的东西-chapter3,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写一个猜数字的游戏,在运行这个程序的时候,输出看起来像:
I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess.
15
Your guess is too low.
Take a guess.
17
Your guess is too high.
Take a guess.
16
Good job! You guessed my number in 4 guesses!
源代码如下,文件命名: guessTheNumber.py:
# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)#在1到20间随机生成一个整数
print('I am thinking of a number between 1 and 20.')# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):#猜6次print('Take a guess.')guess = int(input())if guess < secretNumber:print('Your guess is too low.')elif guess > secretNumber:print('Your guess is too high.')else:break # This condition is the correct guess!if guess == secretNumber:print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:print('Nope. The number I was thinking of was ' + str(secretNumber))
这篇关于猜数字的游戏Python3,用Python自动化无聊的东西-chapter3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!