先呈现的是自己按照游戏的规则编的所谓 Guess My Number Game:
游戏规则:计算机随机给出1到100的任意整数,由参与者去猜,前提是参与者自己估计出猜对需要的次数,如果在次数用完后还未猜中则游戏结束,参与者根据自己需要决定是否要继续参与游戏。
import random
print("\t\t\tWelcome to play the game 'Guess My Number'!")
print("\nI'll give you a random number between 1 and 100.")
print("Give a try to guess it in a few attempts as possible.")
print('\n')
while True:
randomnumber = random.randint(1,100)
times = 1
settimes = int(input("How many times do you think you'll use to guess it right? "))
while settimes > 0:
yourguess = int(input("\nTake a guess:"))
if randomnumber == yourguess and times ==1:
print("\n\tCongratulation!You made it! The number is",randomnumber)
print("\tAnd it only took you",times,"try!")
break
elif randomnumber == yourguess:
print("\n\tCongratulation!You made it! The number is",randomnumber)
print("\tAnd it took you",times,"tries!")
break
elif randomnumber > yourguess:
print("Lower...")
times+=1
settimes-=1
else:
print("Higher...")
times+=1
settimes-=1
if settimes > 0:
print("\n")
else:
print("\nSorry,you have run out of you chance.")
choice = input("\nPlay it again?Yes or No?")
if choice == "yes"or choice == "YES"or choice == "Yes":
#其实这里的if条件语句不需要or就可以达到相同效果,能想到吗?用一个lower()函数就可以了。
# continue
print("Let's start it again!")
#elif choice == "YES":
#print("Let's start it again!")
#elif choice == "Yes":
#print("Let's start it again!")
else:
# continue
print("\nWelcome to you next time!")
print("""\n Have a nice day!""")
break
input("\n\nPress enter key to exit.")
接下来说明 程序里面用到的基本概念有:模块(module)(randint()或randrange()),If条件语句和While循环语句。当然还有之前print()函数的基本用法。
模块(module)是一种可供其他程序调用的代码文件,它们里面的内容都是按照功能相关性组织在一起的。例如用到的random模块里面的函数都是和随机数,随机结果有关的。以random模块为例,引入模块的方法为:import random。其中的randint()及randrange()的使用格式为:random.randint(1,100);random.randrange(100),前者为产生1到100的随机数,后者为产生0到99的随机数。这里对于函数的调用是要先通过模块才可以的,先给出模块名,再加一个句点,然后才是函数名,这种访问方式叫点标记法(dot notation)。
If语句的格式:先写if,再写条件,然后是一个冒号,接下来另起一行写语句块(具有相同缩进的代码构成语句块)。用到else时是因为if后面的条件不满足,此时在else后加一个冒号,接下来是另起一行进行的语句块。当出现多个条件时,会用到elif,它的基本格式和if后面的情况一样。if语句只执行第一次满足条件后的语句块。注意if,elif和else具有相同的缩进。
While循环:while语句是当条件语句为真时,下面的代码块就会被执行,知道条件不满足为止。它的代码结构和if语句相似。当时用while TRUE:时,记得加上break语句,这样可以避免无限循环。其中continue语句的意思是跳过该步及后面的代码,回到循环开始的地方,它可以用在避免特殊情况的执行的情形。例如上面程序中,如果让continue执行,就不会打印后面的内容。
Fortune Cookie游戏:
Fortune Cookie要求如下
要求每次运行都能随机显示出五条灵签中的任意一条
import random
yourwill = input("Start to have a fortune test!Yes or No:")
while True:
if yourwill == "Yes"or yourwill == "YES" or yourwill == "yes":
randomnumber = random.randint(1,5)
if randomnumber == 1:
print("\n恭喜你,将有大好运降临!")
elif randomnumber == 2:
print("\n最近会人见人爱,花见花开哦!")
elif randomnumber == 3:
print("\n现在刻苦的你,注定在不久的将来大展宏图,一展抱负!")
elif randomnumber ==4:
print("\n必定成就非凡!Fighting!!!")
else:
print("\n少做白日梦,天下没有永久的不劳而获!")
print("\nAnyway,you are always so lucky!")
print("\n\tHappy every day!")
again = input("\nPlay it again?Yes or No:")
if again == "Yes"or again == "YES"or again == "yes":
print("\nGood!Let's begin!")
else:
print("\nWish you have a good luck!")
break
else:
print("Wish you have a good luck!")
break
input("\n Press the enter to finish this test!")
掷硬币100次,显示最终的正反面次数!
import random
print("Let's start to toss a coin 100 times! ")
print("you can try to guess how many heads and tails finally")
times = 1
heads = 0
tails = 0
while True:
yourans1 = int(input("Your guess heads'times is:"))
yourans2 = int(input("Your guess tails'times is:"))
times = 1
heads = 0
tails = 0
while times <= 100:
cointype = random.randint(1,2)
if cointype == 1:
heads += 1
else:
tails += 1
times += 1
print("\nFinally the answer is we have",heads,"times of heads!")
print("And",tails,"times of tails!")
if yourans1 == heads and yourans2 == tails:
print("Bravo!You are so amazing to guess it right!")
else:
print("\nProbility is always hard to guess!Don't be sad!")
yourwill = input("\nWant to try it again? Yes or No:")
if yourwill == "yes" or yourwill == "Yes" or yourwill == "YES":
print("\nLet's do it again!")
else:
print("\n\tWell then,Bye!")
break
input("\nPress the enter key to kill the game!")
Guess Number Game的角色互换版本:
import random
print("Let's play a 'Guess Number' game")
print("Please chose a integer randomly form 1 to 100!And do not tell me the number.")
settimes = int(input("Please enter the times that I can use to win the game: "))
input("When you are ready,please press the enter key to start this game!")
randomnum = random.randint(1,100)
randomdown = 1
randomup = 100
times = 1
while True:
print("I guess the number is",randomnum)
judge = input("Is it right number? Yes? Lower or Higher?")
if times <= settimes:
if judge == "Yes" or judge == "yes" or judge == "YES":
print("\nI tried ",times,"times to guess it right! I am a genius!")
print("\tI win!!!")
times += 1
break
elif judge == "Lower" or judge == "LOWER" or judge == "lower":
randomdown = randomnum+1
randomnum = random.randint(randomdown,randomup)
times += 1
else:
randomup = randomnum-1
randomnum = random.randint(randomdown,randomup)
times += 1
else:
print("\n\tI lose!")
break
input("\n\tPress the enter key to end this game!")