实验筛子:
涉及使用知识点:
1.input的使用
num = input('your money :'+str(ffnum)+':how much do you want bet?:')
2.队列
point1=random.randrange(1,7)
point2=random.randrange(1,7)
point3=random.randrange(1,7)
a_list=[point1,point2,point3]
- random.randrange的使用
point3=random.randrange(1,7)
- And 和 or
if summer > 10 and summer <19:
and 即 && or 即 ||
- 需要注意point1是数字,输出时,需要转换为字符串,使用str()
print('the points are[' + str(point1) + ',' + str(point2) + ',' + str(point3)+’]’]
7. `if guess == 'big':`
每一个关键字(例如if else elif while )后,需要加上‘:’
代码:
```handlebars
import random
ffnum =1000
while ffnum > 0:
point1=random.randrange(1,7)
point2=random.randrange(1,7)
point3=random.randrange(1,7)
a_list=[point1,point2,point3]
summer=sum(a_list)
print('<<game starts>>')
print('Big or small:')
guess = input('')
num = input('your money :'+str(ffnum)+':how much do you want bet?:')
print('<<role the dice>>')
if summer > 10 and summer <19:
if guess == 'big':
print('the points are ['+str(point1)+','+str(point2)+','+str(point3)+']:you win '+str(num)+',you have ' +str(ffnum+int(num))+' now')
else:
print('the points are[' + str(point1) + ',' + str(point2) + ',' + str(point3) + ']:you lose'+str(num)+',you have ' +str(ffnum-int(num))+' now')
else:
if guess == 'small':
print('the points are[' + str(point1) + ',' + str(point2) + ',' + str(point3) + ']:you win'+str(num)+',you have ' +str(ffnum+int(num))+'now')
else:
print('the points are[' + str(point1) + ',' + str(point2) + ',' + str(point3) + ']:you lose'+str(num)+',you have ' +str(ffnum-int(num))+' now')
预计实现效果:
参考代码:
1 import random
2 def roll_dice(numbers=3, points=None):
3 print('<<<<< ROLL THE DICE! >>>>>')
4 if points is None:
5 points = []
6 while numbers > 0:
7 point = random.randrange(1,7)
8 points.append(point)
9 numbers = numbers - 1
10 return points
1 def roll_result(total):
2 isBig = 11 <= total <=18
3 isSmall = 3 <= total <=10
4 if isBig:
5 return 'Big'
6 elif isSmall:
7 return 'Small'
1 def start_game():
2 print('<<<<< GAME STARTS! >>>>>')
3 choices = ['Big','Small']
4 your_choice = input('Big or Small :')
5 if your_choice in choices:
6 points = roll_dice()
7 total = sum(points)
8 youWin = your_choice == roll_result(total)
9 if youWin:
10 print('The points are',points,'You win !')
11 else:
12 print('The points are',points,'You lose !')
13 else:
14 print('Invalid Words')
15 start_game() 16 start_game()