本文目錄一覽:
- 1、用python寫猜數字小遊戲
- 2、python猜數遊戲:在程序中預設一個隨機數?
- 3、python語言編寫一個玩猜數的遊戲.由程序產生1~1000間的隨機數,玩遊戲者可輸入最多十次猜數
- 4、用python寫一個猜數遊戲
- 5、用python實現猜數字
- 6、初學python,被作業難到了,做一個猜數遊戲,給十次機會,寫完運行不出來,求大佬看看
用python寫猜數字小遊戲
核心代碼給你,具體的功能還需要自己完善。
import time, random
class GuessNum:
def __init__(self):
self._num = ”
self.input_num = []
self.count = 1 #猜對所用次數
self.sec = 0 #猜對所用時間
self._generate_num()
def _generate_num(self): #產生不重複的四個數字
seq_zton = list(range(10))
for i in range(0, 4):
a = str(random.choice(seq_zton)) #選出一個數字
self._num += a
seq_zton.remove(int(a)) #注意a的類型
self.sec = time.clock() #開始計時
def check_answer(self):
return self._num
def check_input(self):
num_pos, num_value = 0, 0 #位置對和數值對的分別的個數
tmp = input(“Please input the number you guess(No repetition),or ‘c’ to check the answer:”)
if tmp == ‘c’:
print(self.check_answer())
tof = self.check_input()
return tof
elif not tmp.isalnum or not len(tmp) == 4:
print(“Wrong format!”)
tof = self.check_input() #需要優化
return tof
self.input_num = list(tmp)
lst_temp = list(self._num)
if self.input_num == lst_temp: #猜對
self.prt_vic()
return True
for i in lst_temp:
if i in self.input_num:
if lst_temp.index(i) == self.input_num.index(i): #位置也相同
num_pos += 1
num_value += 1
else:
num_value += 1
self.prt_state(num_pos, num_value)
self.count += 1
return False
def prt_state(self, num_pos, num_value):
print(“You’ve got %d numbers with the right position and %d numbers with the right value only” % (num_pos, num_value))
def prt_vic(self):
t = time.clock()
self.sec = t – self.sec
print(“Congratulations!You have successfully got the right number!”)
print(“%d times and %.2f sec in total to get the right answer” % (self.count, self.sec))
gn = GuessNum()
while True:
ss = gn.check_input()
if ss:
b = input(“Continue? y/n:”)
if b == ‘n’:
break
else:
gn = GuessNum()
continue
python猜數遊戲:在程序中預設一個隨機數?
import random
num = random.randint(0, 100) #隨機數
N = 0 #訪問次數
while True:
N += 1
x = int(input())
if x == num:
print(“猜中了,用了{}次”.format(N))
break
if x num:
print(“太大了”)
else:
print(“太小了”)
python語言編寫一個玩猜數的遊戲.由程序產生1~1000間的隨機數,玩遊戲者可輸入最多十次猜數
這個有驗證碼的意思,
不過樓主的python經驗少了, 如果上面輸入的不是數字, 那麼程序會報錯的, 別有用心的人就能知道程序是如何編寫的了, 這樣不好啊
以後遇到python方面的問題, 可以幫忙搞定。
樓主看看網名
用python寫一個猜數遊戲
#-*-coding:utf-8-*-
import random
import sys
if sys.version[0]==’3′: raw_input=input
x=[1,2,3,4,5,6,7,8,9]
result=set(x)
while True:
y=x[:]
random.shuffle(y)
y=y[:random.randint(1,len(x))]
print(“%s 中有你想要的嗎?”%y)
answer=raw_input()
if answer.upper() in [‘Y’,’YES’]:
result=result.intersection(y)
if len(result)==1:
print(“answer is :%s”%(list(result))[0])
break
用python實現猜數字
a = ‘1459’
for x in range(7):
j = 0
b = str(input(‘請輸入4個數字:’))
# 判斷輸入的是否為4位
if len(b)!=4:
print(“輸入有誤”)
break
# 判斷輸入的是否為數字
try:
float(b)
except:
print(“輸入有誤”)
break
for i in range(4):
if b[i]==a[i]:
j=j+1
print(str(j)+’A’+str(4-j)+’B’)
if j==4:
print(“恭喜你贏了”)
break
print(“遊戲結束”)
初學python,被作業難到了,做一個猜數遊戲,給十次機會,寫完運行不出來,求大佬看看
import random
target=random.randint(1,1000)
count=0
while True:
try:
guess=eval(input(“猜猜這個數是什麼,一共有10次機會哦”))
except:
continue
print(“請輸入一個整數”)
if guesstarget:
print(“猜小了”)
count=+1
elif guesstarget:
print(“猜大了”)
count=+1
elif count==10:
print(“機會用完了,歡迎下次再來!”)
break
else:
print(“猜對了,正確答案為”,target,”/n”,”一共猜了{}次”.format(count))
break
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/189109.html