使用random變量隨機生成一個1到100之間的數python
採集用戶所輸入的數字,若是輸入的不符合要求會讓用戶從新輸入。git
輸入符合要求,遊戲開始。若是數字大於隨機數,輸出數字太大;若是小於隨機數,輸出數字過小dom
猜對,輸出數字正確,猜的次數;並詢問是否繼續遊戲spa
用戶回答y(yes)表示繼續玩code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import
random
rnum
=
random.randint(
1
,
100
)
count
=
0
while
True
:
num
=
input
(
'please enter a number(1,100): \n'
).strip()
if
num.isdigit():
num
=
int
(num)
count
+
=
1
if
num
=
=
rnum:
print
(
'yes,{} is right;you guess {} times'
.
format
(num,count))
ask
=
input
(
'would you like play again(y/n): \n'
).strip().lower()
if
ask
=
=
'y'
:
continue
else
:
break
break
elif
num > rnum:
print
(
'you number is too lager!'
)
continue
else
:
print
(
'you number is too small!'
)
continue
else
:
print
(
'you number is invalid,please enter again'
)
continue
|