簡體   English   中英

如何讓我的函數打印我的代碼?

[英]How do I get my function to print my code?

所以我正在制作一個擲骰子直到它變成蛇眼的程序。 問題是,每當我運行我的代碼時,它都不會打印任何內容。 當它之前運行時,它是一個無限循環。 我會把我的代碼放在下面。 我知道肯定有一種更簡單的方法可以做到這一點,但我只是希望它能夠首先運行。

def snakeEyes():
#Simulate dice
#Variables to store integers
  diceCount = 0
  dice1 = 3
  dice2 = 4
  diceSum = dice1 + dice2

  while(diceSum != 2):
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    diceCount = diceCount + 1

# if both 1's - snake eyes
    if(dice1 + dice2 == 2):
      combo_name = "snake eyes"
  # if both 6's - boxcars
    elif(dice1 + dice2 == 12):
      combo_name = "boxcars"
  #if 7 --> natural
    elif(diceSum == 7):
      combo_name = "natural"
  #if sum % 2 == 0 -- we got an even
    # if dice values are same -- "hard"
    # else - easy
    # turn the sum number into text number
    elif(diceSum % 2 == 0):
      if(dice1 == dice2):
        combo_name = "hard " + str(diceSum)
      else:
        combo_name = "easy " + str(diceSum)
  # else - have an odd number
  # figure out which odd name we are using
    else:
      if(diceSum == 3):
        combo_name = "ace deuce"
      elif(diceSum == 5):
        combo_name = "fever five"
      elif(diceSum == 9):
        combo_name = "nina"
      elif(diceSum == 11):
        combo_name = "yo-leven"

  #Print line with dice values and names
      print ("Dice1: " + str(dice1)+ "  Dice 2: " + str(dice2)+ "   " + combo_name)

  print("The dice rolled " + str(diceCount) + " times before it got snake eyes.")


print(snakeEyes())

diceSumwhile循環之前聲明,並且永遠不會再次重新分配,因此它的值始終為7 在循環中,您應該添加以下行: diceSum = dice1 + dice2

編輯: diceCount是因為這個圖片

print處於else狀態。

您的函數已經打印,所以在調用函數時不要使用print()而是嘗試這樣: snakeEyes()

這里的主要問題是你永遠不會在 while 循環中為 diceSum 重新賦值,因此 diceSum 永遠是 7。

import random


def snakeEyes():
    # Simulate dice
    # Variables to store integers
    diceCount = 0
    diceSum = 0
    while(diceSum != 2):
        dice1 = random.randint(1, 6)
        dice2 = random.randint(1, 6)
        diceSum = dice1 + dice2
        diceCount = diceCount + 1

        # if both 1's - snake eyes
        if diceSum == 2:
            combo_name = "snake eyes"
        # if both 6's - boxcars
        elif diceSum == 12:
            combo_name = "boxcars"
        # if 7 --> natural
        elif diceSum == 7:
            combo_name = "natural"
        # if sum % 2 == 0 -- we got an even
        # if dice values are same -- "hard"
        # else - easy
        # turn the sum number into text number
        elif diceSum % 2 == 0:
            if dice1 == dice2:
                combo_name = "hard " + str(diceSum)
            else:
                combo_name = "easy " + str(diceSum)
        # else - have an odd number
        # figure out which odd name we are using
        else:
            if diceSum == 3:
                combo_name = "ace deuce"
            elif diceSum == 5:
                combo_name = "fever five"
            elif diceSum == 9:
                combo_name = "nina"
            elif diceSum == 11:
                combo_name = "yo-leven"

        # Print line with dice values and names
        print("Dice1: " + str(dice1) + "  Dice 2: " + str(dice2) + "   " + combo_name)

    print("The dice rolled " + str(diceCount) + " times before it got snake eyes.")


snakeEyes()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM