#This is a Guess The Number game. import random#赋值过程自动创建变量 guessesTaken &#61;0print("Hello! What is your name?") myName &#61;input()number &#61; random.randint(1,20) print("Well,"&#43; myName &#43;", I an thinking of a number between 1 and 20.")for i inrange(6):print("Take a guess.")guess &#61;input()guess &#61;int(guess)if guess < number:print("Your guess is too low.")if guess > number:print("Your guess is too high.")if guess &#61;&#61; number:break#跳出for循环if guess &#61;&#61; number:guessesTaken &#61; iguessesTaken &#61;str(guessesTaken)print("Good job,"&#43; myName &#43;"Your guessed my number in "&#43; guessesTaken &#43;" guesses!")if guess !&#61; number:number &#61;str(number)print("Nope. The number I was thinking of was"&#43; number &#43;".")
运行结果&#xff1a;
runfile(&#39;D:/Z/Python/Python游戏编程/游戏编程代码/猜数字.py&#39;, wdir&#61;&#39;D:/Z/Python/Python游戏编程/游戏编程代码&#39;) Hello! What is your name?低头写作业 Well,低头写作业, I an thinking of a number between 1and20. Take a guess.13 Your guess is too low. Take a guess.16 Your guess is too low. Take a guess.18 Your guess is too high. Take a guess.177 Your guess is too high. Take a guess.17 Good job,低头写作业Your guessed my number in4 guesses!
for i inrange(6):print("Take a guess.")guess &#61;input()guess &#61;int(guess)if guess < number:print("Your guess is too low.")if guess > number:print("Your guess is too high.")if guess &#61;&#61; number:break#跳出for循环
a &#61;21 b &#61;10 c &#61;0if( a &#61;&#61; b ):print("1 - a 等于 b") else:print("1 - a 不等于 b")if( a !&#61; b ):print("2 - a 不等于 b") else:print("2 - a 等于 b")if( a < b ):print("3 - a 小于 b") else:print("3 - a 大于等于 b")if( a > b ):print("4 - a 大于 b") else:print("4 - a 小于等于 b")# 修改变量 a 和 b 的值 a &#61;5; b &#61;20; if( a <&#61; b ):print("5 - a 小于等于 b") else:print("5 - a 大于 b")if( b >&#61; a ):print("6 - b 大于等于 a") else:print("6 - b 小于 a")
输出结果
1- a 不等于 b 2- a 不等于 b 3- a 大于等于 b 4- a 大于 b 5- a 小于等于 b 6- b 大于等于 a
if guess &#61;&#61; number:guessesTaken &#61; iguessesTaken &#61;str(guessesTaken)print("Good job,"&#43; myName &#43;"Your guessed my number in "&#43; guessesTaken &#43;" guesses!")if guess !&#61; number:number &#61;str(number)print("Nope. The number I was thinking of was"&#43; number &#43;".")