"def函数在每个(while)循环中重复自己两次,即使我每次只调用它们一次"

2024-10-01 00:17:59 发布

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

我正在为一个学校项目做一个游戏,有两个玩家,每个人掷2个骰子,每轮结束后计算分数。我为每个玩家制作了掷骰子的函数,但是当我在while循环中调用这个函数(5轮运行5次)时,它会循环5次,但是每次都会出于某种原因重复两次。我通读了一遍,但找不出哪里出错了。我认为这可能与函数中的if-elif语句有关,但我不确定,也不知道如何修复它。我将非常感谢任何帮助,因为我需要完成这项工作并交上来。提前谢谢

import random
#Function for Player ones rolls
def rollP1():
    #Player 1 rolls twice
    print("\n\nPlayer 1's rolls:")
    rollOne1 = random.randint(1,6)
    rollTwo1 = random.randint(1,6)
    #Checking if player 1 has a double, even total, or odd total
    #if player has a double, player gets a bonus roll
    if rollOne1 == rollTwo1:
        print("You got a double. ", rollOne1, "and ", rollTwo1,", have a 
        bonus 
        roll!")
        rollThree1 = random.randint(1,6)
        print(rollThree1)
        rollTotal1 = rollOne1 + rollTwo1 + rollThree1
        print("Your total score for this round is ", rollTotal1, ". Well 
        Done.")
    #if player gets an even total, score increases by 10
    elif (rollOne1 + rollTwo1)%2 == 0:
        rollTotal1 = rollOne1 + rollTwo1 + 10
        print("You got ", rollOne1, " and ", rollTwo1, ", your total is an 
        even number, plus 10 points. Your total for this round is now ", 
        rollTotal1, ". Well Done.")
    #if player gets an odd total, score decreases by 5
    elif (rollOne1 + rollTwo1)%2 != 0:
        rollTotal1 = (rollOne1 + rollTwo1) - 5
        print("You got ", rollOne1, " and ", rollTwo1, ". Unlucky, your 
        total is an odd number. Minus 5 points Your total for this round is 
        now ", rollTotal1, ". Better luck next time.")
    #Returning the total score for Player one for the round.
    return rollTotal1


#Function for Player twos' rolls
def rollP2():
    #Player 2 rolls twice
    print("\nPlayer 2's rolls:")
    rollOne2 = random.randint(1,6)
    rollTwo2 = random.randint(1,6)
    #Checking if player 2 has a double, even total, or odd total
    #if player has a double, player gets a bonus roll
    if rollOne2 == rollTwo2:
        print("You got a double. ", rollOne2," and", rollTwo2, ", have a 
        bonus roll!")
        rollThree2 = random.randint(1,6)
        print(rollThree2)
        rollTotal2 = rollOne2 + rollTwo2 + rollThree2
        print("Your total score for this round is ", rollTotal2, ". Well 
        Done.")
    #if player gets an even total, score increases by 10
    elif (rollOne2 + rollTwo2)%2 == 0:
        rollTotal2 = rollOne2 + rollTwo2 + 10
        print("You got ", rollOne2, " and ", rollTwo2, ". Your total is an 
        even number, plus 10 points. Your total for this round is now ", 
        rollTotal2, ". Well Done.")
    #if player gets an odd total, score decreases by 5
    elif (rollOne2 + rollTwo2)%2 != 0:
        rollTotal2 = (rollOne2 + rollTwo2) - 5
        print("You got ", rollOne2, " and ", rollTwo2, ". Unlucky, your 
        total is an odd number. Minus 5 points Your total for this round is 
        now ", rollTotal2, ". Better luck next time")
    #Returning the total score for Player two for the round.
    return rollTotal2


Total1 = 0
Total2 = 0
rounds = 1

while rounds < 6:
    print("Round ", rounds)
    rollP1()
    rollP2()
    rollTotal1 = rollP1()
    rollTotal2 = rollP2()
    print("Player 1's score for this round is ", rollTotal1, " and player 
    2's score for this round is ", rollTotal2)
    Total1 = Total1 + rollTotal1
    Total2 = Total2 + rollTotal2
    print("Player 1's total so far is ", Total1, ", Player 2's total so far 
    is ", rollTotal2, ". Round Over\n\n")
    rounds = rounds + 1

Tags: forifistotalscoreplayerprintround
1条回答
网友
1楼 · 发布于 2024-10-01 00:17:59

试试这个。刚刚挂了第二个电话

import random
#Function for Player ones rolls
def rollP1():
    #Player 1 rolls twice
    print("\n\nPlayer 1's rolls:")
    rollOne1 = random.randint(1,6)
    rollTwo1 = random.randint(1,6)
    #Checking if player 1 has a double, even total, or odd total
    #if player has a double, player gets a bonus roll
    if rollOne1 == rollTwo1:
        print("You got a double. ", rollOne1, "and ", rollTwo1,", have a bonus roll!")
        rollThree1 = random.randint(1,6)
        print(rollThree1)
        rollTotal1 = rollOne1 + rollTwo1 + rollThree1
        print("Your total score for this round is ", rollTotal1, ". Well Done.")
    #if player gets an even total, score increases by 10
    elif (rollOne1 + rollTwo1)%2 == 0:
        rollTotal1 = rollOne1 + rollTwo1 + 10
        print("You got ", rollOne1, " and ", rollTwo1, ", your total is an even number, plus 10 points. Your total for this round is now ", rollTotal1, ". Well Done.")
    #if player gets an odd total, score decreases by 5
    elif (rollOne1 + rollTwo1)%2 != 0:
        rollTotal1 = (rollOne1 + rollTwo1) - 5
        print("You got ", rollOne1, " and ", rollTwo1, ". Unlucky,  total is an odd number. Minus 5 points Your total for this round now ", rollTotal1, ". Better luck next time.")
    #Returning the total score for Player one for the round.
    return rollTotal1


#Function for Player twos' rolls
def rollP2():
    #Player 2 rolls twice
    print("\nPlayer 2's rolls:")
    rollOne2 = random.randint(1,6)
    rollTwo2 = random.randint(1,6)
    #Checking if player 2 has a double, even total, or odd total
    #if player has a double, player gets a bonus roll
    if rollOne2 == rollTwo2:
        print("You got a double. ", rollOne2," and", rollTwo2, ", have a bonus roll!")
        rollThree2 = random.randint(1,6)
        print(rollThree2)
        rollTotal2 = rollOne2 + rollTwo2 + rollThree2
        print("Your total score for this round is ", rollTotal2, ". Well Done.")
    #if player gets an even total, score increases by 10
    elif (rollOne2 + rollTwo2)%2 == 0:
        rollTotal2 = rollOne2 + rollTwo2 + 10
        print("You got ", rollOne2, " and ", rollTwo2, ". Your total is an even number, plus 10 points. Your total for this round is now ", rollTotal2, ". Well Done.")
    #if player gets an odd total, score decreases by 5
    elif (rollOne2 + rollTwo2)%2 != 0:
        rollTotal2 = (rollOne2 + rollTwo2) - 5
        print("You got ", rollOne2, " and ", rollTwo2, ". Unlucky, your total is an odd number. Minus 5 points Your total for this round is now ", rollTotal2, ". Better luck next time")
    #Returning the total score for Player two for the round.
    return rollTotal2


Total1 = 0
Total2 = 0
rounds = 1

while rounds < 6:
    print("Round ", rounds)
    rolledP1 = rollP1()
    rolledP2 = rollP2()
    rollTotal1 = rolledP1
    rollTotal2 = rolledP1
    print("Player 1's score for this round is ", rollTotal1, " and player 2's score for this round is ", rollTotal2)
    Total1 = Total1 + rollTotal1
    Total2 = Total2 + rollTotal2
    print("Player 1's total so far is ", Total1, ", Player 2's total so far is ", rollTotal2, ". Round Over\n\n")
    rounds = rounds + 1

相关问题 更多 >