>第一次在这里写。我正在用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)
print("Roll or Quit(r or q)")
现在每次我输入 r 时,它都会一遍又一遍地生成相同的数字。我只想每次都改变它。
我希望它每次都更改号码,请帮忙我问我的教授,但这是他告诉我的。"我想你必须弄清楚"我的意思是我希望我能,我已经一遍又一遍地浏览了我的笔记,但我没有任何关于如何做到这一点的东西:-/
顺便说一下,这就是它向我展示程序的方式
COP 1000
Let's play a game of Chicken!
Your score so far is 0
Roll or Quit(r or q)r
1
r
1
r
1
r
1
我想发布一张图片,但它不允许我
我
只想对所有回答我问题的人说声谢谢! 你的每一个回答都是有帮助的! **感谢你们,我将按时完成我的项目!谢谢
只需使用:
import random
dice = [1,2,3,4,5,6] #any sequence so it can be [1,2,3,4,5,6,7,8] etc
print random.choice(dice)
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 循环---教程点
关于Python 3的挑剔部分(假设这就是你正在使用的)是缺乏raw_input
。使用input
,无论用户输入什么,都会被评估为Python代码。因此,用户必须输入"q"或"r"。但是,避免用户错误的一种方法(如果玩家输入只是q
或r
,而不带引号)是用这样的值初始化这些变量。
不确定什么类型的骰子有 8 个数字,我用了 6 个。
一种方法是使用随机播放。
import random
dice = [1,2,3,4,5,6]
random.shuffle(dice)
print(dice[0])
每次,它都会随机洗牌列表并取第一个。
蟒蛇掷骰子它要求一个 d(int) 并返回一个介于 1 和 (d(int)) 之间的随机数。它返回不带 d 的骰子,然后打印随机数。它可以做2d6等。如果您键入 q 或退出,它会中断。
import random
import string
import re
from random import randint
def code_gate_3(str1):
if str1.startswith("d") and three == int:
return True
else:
return False
def code_gate_1(str1):
if str1.startswith(one):
return True
else:
return False
def code_gate_2(str2):
pattern = ("[0-9]*[d][0-9]+")
vvhile_loop = re.compile(pattern)
result = vvhile_loop.match(str1)
if result:
print ("correct_formatting")
else:
print ("incorrect_formattiing")
while True:
str1 = input("What dice would you like to roll? (Enter a d)")
one, partition_two, three = str1.partition("d")
pattern = ("[0-9]*[d][0-9]+")
if str1 == "quit" or str1 == "q":
break
elif str1.startswith("d") and three.isdigit():
print (random.randint(1, int(three)))
print (code_gate_2(str1))
elif code_gate_1(str1) and str1.partition("d") and one.isdigit():
for _ in range(int(one)):
print (random.randint(1, int(three)
print (code_gate_2(str1))
elif (str1.isdigit()) != False:
break
else:
print (code_gate_2(str1))
print ("Would you like to roll another dice?")
print ("If not, type 'q' or 'quit'.")
print ("EXITING>>>___")
这是最简单的答案之一。
import random
def rolling_dice():
min_value = 1
max_value = 6
roll_again = "yes"
while roll_again == "yes" or roll_again == "Yes" or roll_again == "Y" or roll_again == "y" or roll_again == "YES":
print("Rolling dices...")
print("The values are...")
print(random.randint(min_value, max_value))
print(random.randint(min_value, max_value))
roll_again = input("Roll the dices again? ")
rolling_dice()