猜數字遊戲
系統生成乙個100以內的隨機整數,
玩家有6次機會進行猜猜看,每次猜測都有反饋(猜大了,猜小了,猜對了-結束)
6次中,猜對了,玩家贏了。
否則系統贏了
#!/usr/bin/env python
import random
secret=random.randint(1,100)
guess=0
tries=0
print "this game is to guess a number for you!"
print " it is a number from 1 to 99,i'll give you 6 times to try. "
while guess != secret and tries < 6:
guess=input("please input your guess number: ")
if guess < secret:
print "====your guess is too low !====\n"
elif guess > secret:
print "====your guess is too high!====\n"
tries=tries+1
if guess == secret:
print "congratulations to you! your guess is right ! "
else:
print "no more guesses! good luck next time for you!"
print "the secret number was",secret
python 猜數字遊戲
本文,我們通過乙個猜數字遊戲,鞏固一下python中迴圈的使用。使用python x,y 如下 from random import randint x randint 0,100 在閉區間 0,100 內隨機產生乙個整數 print x d x go yes while go yes 當條件不成立...
python猜數字遊戲
引入隨機數包 import random 1.生成乙個隨機答案數 2.輸入要猜的數字 3.對比猜的數字和正確答案 3.1猜大了 3.2猜小了 3.3猜對了 3.3.1繼續玩 3.3.2退出 number random.randint 0 100 宣告變數用來表示使用者猜測的次數 count 0 a ...
猜數字遊戲 python
使用for迴圈隨機生成乙個0 100之間的數字,然後使用者輸入乙個整數,判斷是否正確,若正確,則返回猜對了,若偏大則返回太大了,偏小則返回太小了,程式可執行五次 from random import randint x randint 0,100 for count in range 0,5 pri...