簡體   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