1. 程式人生 > 實用技巧 >一聽就懂:用Python做一個超簡單的小遊戲

一聽就懂:用Python做一個超簡單的小遊戲

寫它會用到

while 迴圈
random 模組
if 語句
輸入輸出函式


原始碼先丟擲來

import random #匯入random模組,用來產生隨機數
times = 10 #設定我們的答題次數
secret = random.randint(1,100) #隨機給random一個1~9的數字,再給secret賦值
print('---------------Python要和你玩猜數字遊戲---------------')
print('猜一個1-100之間的數')
guess = 0 #設定guess的值
while (guess != secret) and (times > 0): #結束條件,要麼次數用完,要麼答對。
    guess = int(input("猜猜我想的是幾:")) #使用者輸入,並轉換為整數
    times = times - 1 #使用者每輸入一次,猜數次數就-1
    if guess == secret: #如果使用者輸入對了
        print("恭喜你猜對了!") 
        print("哼,猜中了也沒有獎勵!")
        break #跳出迴圈
    else:
        if guess > secret:
            print("大了大了~~~")# 之所以不在這裡設定重輸,是因為在if times > 0: 中已經弄過了,這樣避免輸兩次,也避免次數已用完還讓重輸
        else:
            print("小了小了-_-")
    if times > 0: #判斷次數是否用完
        print("再試一次吧:", end=" ")
    else:
        print("機會用光咯T_T")
        print("我想的是"+str(secret)+"!") #揭曉答案
print("遊戲結束,不玩啦^_^")

知識講解

random

#0## 介紹
Python中的隨機數操作較簡單,不像其他高階語言那樣必須用特定的公式才能產生

語法

import random #匯入模組
random.方法() #呼叫方法

方法引數表

方法作用
randint(一個整數,另一整數) 產生從一個整數-另一整數的隨機整數
uniform(a,b) 產生 a 到 b之間的隨機浮點數

目前我們用到的是randint()方法。

舉例

>>> import random
>>> random.randint(0,10)
5
>>> random.randint(0,10)
2
>>> 

while

語法

while 迴圈條件:
	語句

舉例

num = 0
while num<3:#如果num小於3,就進入迴圈
	num+=1 #等同num=num+1
	print(num)

圖:


拆分程式碼講解

產生隨機數

import random
random.randint(0,100)

執行結果:

>>> random.randint(0,100)
50
>>> random.randint(0,100)
28
>>> random.randint(0,100)
35
>>> random.randint(0,100)
15
>>> random.randint(0,100)
10
>>> random.randint(0,100)
27
>>> random.randint(0,100)
89
>>> random.randint(0,100)
43
>>> random.randint(0,100)
90
>>> random.randint(0,100)
16
>>> random.randint(0,100)
80
>>> 

現在我們可以產生隨機數了,但是隻有呼叫一次方法才能產生一次隨機數,怎麼辦呢?對了,可以用迴圈來反覆做相同的事。


迴圈產生隨機數

import random
while True:
	random.randint(0,100)

執行結果:

68
25
51
44
75
21
70
2
4
25
95
34
19
59
63
98
93
15
0
60
69
33
2
84
38
54
30
64
69
94
94
8
24
95
80
69
83
20
48
92
24
48
51
77
25
23
68
70
84
34
56
86
30
39
12
90
0
36
31
23
34
51
60
26
81
63
88
74
40
55
69
47
44
8
38
6
63
3
72
76
8
100
88
20
99
49
47
35
80
7
49
6
78
42
20
44
49
49
71
53
55
67
51
55
39
87
98
19
61
70
76
58
94
47
73
10
22
29
89
95
33
98
44
15
29
4
78
16
13
71
78
43
18
43
29
64
87
8
66
91
55
12
81
8
48
20
23
5
6
Traceback (most recent call last):
  File "<pyshell#3>", line 2, in <module>
    random.randint(0,100)
KeyboardInterrupt

>>> 

可以看到,它一直輸出隨機數,這並不是我們想要的,我們想要的是使用者輸入一個數,和這個隨機數比對

增加迴圈條件和簡單的判斷

對上述採取的方法是:把產生的隨機數存入變數,進行判斷、比對

import random
secret = random.randint(0,100) #產生隨機整數
guess = input('猜猜我想的是幾:')#獲取使用者輸入
while guess!=secret:#當用戶輸入不等於產生的數時,進入迴圈
	if guess>secret:#當用戶輸入的數大於隨機數時	
		print("大了大了@_@")#列印提示文字
	else:#當用戶輸入的數小於隨機數時
		print("小了小了>_<")#列印提示文字
print("恭喜你答對了!")
print("哼,對了也沒有獎勵!")
print('遊戲結束,不玩了~')

看似沒有問題,我們來執行一下
執行後發現報錯了:

猜猜我想的是幾:12
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/aa.py", line 5, in <module>
    if guess>secret:#當用戶輸入的數大於隨機數時
TypeError: '>' not supported between instances of 'str' and 'int'

哦,原來是沒有轉換型別,input返回的是字串,字串不能和整數比較

改後:

import random
secret = random.randint(0,100) #產生隨機整數
guess = int(input('猜猜我想的是幾:'))#獲取使用者輸入,並轉化為整數
while guess!=secret:#當用戶輸入不等於產生的數時,進入迴圈
	if guess>secret:#當用戶輸入的數大於隨機數時	
		print("大了大了@_@")#列印提示文字
	else:#當用戶輸入的數小於隨機數時
		print("小了小了>_<")#列印提示文字
print("恭喜你答對了!")
print("哼,對了也沒有獎勵!")
print('遊戲結束,不玩了~')

執行:

小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
小了小了>_<
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/aa.py", line 8, in <module>
    print("小了小了>_<")#列印提示文字
KeyboardInterrupt
>>> 

還是有問題!

解決報錯&增加輸錯重輸功能、限制使用者輸入次數功能

同學們想想,當列印提示語後,使用者沒有重輸,會一直滿足那個條件,就會迴圈列印。
那麼我們可以增加重輸功能,當輸的不對時,就重輸,就不會迴圈列印了;
新增限制使用者輸入功能,這樣就避免使用者一直猜

1.當然,因為又加了一個功能,答對了的提示就不能放在迴圈外面了,
2.之所以不在列印大了小了設定重輸,是因為在if times > 0: 中已經弄過了,這樣避免輸兩次,也避免次數已用完還讓重輸

import random #匯入random模組,用來產生隨機數
times = 10 #設定我們的答題次數
secret = random.randint(1,100) #隨機給random一個1~9的數字,再給secret賦值
print('---------------Python要和你玩猜數字遊戲---------------')
print('猜一個1-100之間的數')
guess = 0 #設定guess的值
while (guess != secret) and (times > 0): #結束條件,要麼次數用完,要麼答對。
    guess = int(input("猜猜我想的是幾:")) #使用者輸入,並轉換為整數
    times = times - 1 #使用者每輸入一次,猜數次數就-1
    if guess == secret: #如果使用者輸入對了
        print("恭喜你猜對了!") 
        print("哼,猜中了也沒有獎勵!")
        break #跳出迴圈
    else:
        if guess > secret:
            print("大了大了~~~")# 之所以不在這裡設定重輸,是因為在if times > 0: 中已經弄過了,這樣避免輸兩次,也避免次數已用完還讓重輸
        else:
            print("小了小了-_-")
    if times > 0: #判斷次數是否用完
        print("再試一次吧:", end=" ")
    else:
        print("機會用光咯T_T")
        print("我想的是"+str(secret)+"!") #揭曉答案
print("遊戲結束,不玩啦^_^")

效果


缺陷

沒有設定輸入限制,如果使用者輸入的是字母,那int轉換就會報錯;這個我就不寫了,有興趣的同學可以在評論區寫一下。有什麼不懂的地方,也歡迎在評論區提問

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯絡我們以作處理

想要獲取更多Python學習資料可以加
QQ:2955637827私聊
或加Q群630390733
大家一起來學習討論吧!