试着选择骰子和边

2024-03-29 13:48:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图生成能够有一个骰子数,然后选择骰子上的边数这是我的代码我该如何使这一工作

print("\n") #Creates new program
print("Welcome...")
print("You are are playing a game of dice, this is a two player game, who  ever rolls the highest number wins")
name1 = input("What is Player 1's Name?") #Inputs "player 1's" name 
print("Hello", name1)
name2 = input("What is Player 2's Name?")
print("Greetings",name2)
from random import randint
Dice = input("Please select number of dice you would like to use (atleast 1)")
Sides = input("Please select number of sides on dice you would like to use")
print("You have selected", Dice," dice and", Sides,"Sides")
Roll11 = int(randint(Dice,Sides))
Roll12 = int(randint(Dice,Sides))
print ("Player 1's Roll")
print(Roll11)
print ("Player 2's Roll")
print (Roll12)

Tags: ofyougamenumberinputis骰子dice
2条回答

试试这个:

   print("Welcome...")
    print("You are are playing a game of dice, this is a two player game, who  ever rolls the highest number wins")
    name1 = input("What is Player 1's Name?") #Inputs "player 1's" name 
    print("Hello", name1)
    name2 = input("What is Player 2's Name?")
    print("Greetings",name2)
    from random import randint
    Dice = int(input("Please select number of dice you would like to use (atleast 1)"))
    Sides = int(input("Please select number of sides on dice you would like to use"))
    print("You have selected", Dice," dice and", Sides,"Sides")
    Roll1=0
    Roll2=0
    for i in range(Dice):
        Roll1 += randint(1,Sides)
        Roll2 += randint(1,Sides)
    print ("Player 1's Roll")
    print(Roll1)
    print ("Player 2's Roll")
    print (Roll2)

更改:首先,代码中不需要第一行。其次,您将int应用于错误的行。randint返回int,但input返回string。第三,在DiceSides之间生成一个随机数。我修改为在1和Sides之间生成Dice随机数

我知道,问题是这条线:

Roll11 = int(randint(Dice,Sides))

您正在尝试将字符串值作为整数参数传递。你知道吗

试着这样使用:

Roll11 = randint(int(Dice), int(Sides))

相关问题 更多 >