前言
有的人可能學過一點Python基礎,但不知道干什么好,今天就教大家做一個簡單的小游戲
文章目錄
- 前言
- 寫它會用到
- 原始碼先拋出來
- 知識講解
- random
- 語法
- 方法引數表
- 舉例
- while
- 語法
- 舉例
- 拆分代碼講解
- 產生亂數
- 回圈產生亂數
- 增加回圈條件和簡單的判斷
- 解決報錯&增加輸錯重輸功能、限制用戶輸入次數功能
- 效果
- 缺陷
寫它會用到
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轉換就會報錯;這個我就不寫了,有興趣的同學可以在評論區寫一下,有什么不懂的地方,也歡迎在評論區提問
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/238557.html
標籤:python
上一篇:自動填寫今日校園
