掷骰子python代码_用Python模拟掷骰子?

import random

computer= 0 #Computer Score

player= 0 #Player Score

print("COP 1000 ")

print("Let's play a game of Chicken!")

print("Your score so far is", player)

r= random.randint(1,8) # this only gets called once, so r is always one value

print("Roll or Quit(r or q)")

你的代码有很多错误。这只起一次作用,因为它不在一个循环中。

改进后的代码:from random import randint

computer, player, q, r = 0, 0, 'q', 'r' # multiple assignment

print('COP 1000') # q and r are initialized to avoid user error, see the bottom description

print("Let's play a game of Chicken!")

player_input = '' # this has to be initialized for the loop

while player_input != 'q':

player_input = raw_input("Roll or quit ('r' or 'q')")

if player_input == 'r':

roll = randint(1, 8)

print('Your roll is ' + str(roll))

# Whatever other code you want

# I'm not sure how you are calculating computer/player score, so you can add that in here

while循环在其下执行所有操作(缩进),直到语句变为false。因此,如果播放器输入q,它将停止循环,并转到程序的下一部分。见:Python Loops --- Tutorials Point

Python 3的挑剔之处在于缺少raw_input。使用input,用户输入的任何内容都将被计算为Python代码。因此,用户必须输入“q”或“r”。但是,避免用户错误(如果播放器只输入q或r,不带引号)的方法是用这些值初始化这些变量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值