繁体   English   中英

掷骰子游戏:奇数得到一个小数,偶数得到一个红利

[英]dice rolling game: odd sum gets a malus, even sum a bonus

我必须做一个骰子游戏。 下面是我到目前为止实际掷骰子部分的代码。

如果骰子的总数是偶数,则必须加 10 分。 但是,如果总数是奇数,则必须扣除5分。

  import random

  print("player 1, please roll dice")
  rolldice=input("would you like to roll dice?")
  if rolldice=="yes":
     roll1 = random.randint(1,6)

  roll2=random.randint(1,6)

  print("You rolled",roll1)
  print("you rolled",roll2)
  if roll1+roll2="2,4,6,8,10,12":
      print("roll1+roll2+10"):
import random

print("player 1,please roll dies")
rolldice=input("would you like to roll dies?")
if rolldice=="yes":
    roll1 = random.randint(1,6)

    roll2=random.randint(1,6)

   print("You rolled",roll1)
   print("you rolled",roll2)
   res = roll1 + roll2
   if (roll1+roll2) % 2 == 0:
       res += 10
   else:
       res -= 5
   print(res):

我在代码中添加了注释来解释代码中的错误以及修复它们的各种方法

import random   #indentation must be fixed in your code
print("player 1,please roll dice")
rolldice=input("would you like to roll dice?")
if rolldice=="yes":
     roll1 = random.randint(1,6)
     roll2=random.randint(1,6)
     print("You rolled",roll1)
     print("you rolled",roll2)
     if roll1+roll2 %2==0:      #here % gives reminder of division,this is an
         print(roll1+roll2+10)       #easier method or it should be roll1+roll2 in [2,4,6,8,10,12] (the list shouldn't be in quotations, that would mean that it is a string,not a list)
     else:         # if it isn't an even no, it will obviously be odd, so using else
         print(roll1+roll2-5)

另外:您在最后一行的 print 语句之后添加了:语法错误: :仅在存在不同缩进级别的文本块时使用。 即,for for循环、 ifelseelif

import random

print("player 1, please roll dies")
rolldice = input("would you like to roll dies?")
if rolldice == "yes":
    roll1 = random.randint(1, 6)
    roll2 = random.randint(1, 6)

    print("You rolled", roll1)
    print("you rolled", roll2)
    if (roll1 + roll2) % 2 == 0:
        print(roll1 + roll2 + 10)
    else:
        print(roll1 + roll2 - 5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM