尽管满足条件,但我的if语句条件仍未正确触发

时间:2019-05-27 02:37:32

标签: python

我正在通过学​​习一些小型项目来使自己熟悉,从而学习如何使用Python进行编程。我陷入一个小问题,尽管程序执行过程中满足了条件,但if语句仍未正确触发。这是我自己制作的骰子模拟项目。

由于我仍然是新手,但我正在学习,所以目前无法想到任何其他方法

import random
import time

print("Welcome to the dice simulator, lets find out how lucky you are!")

"\n"

min = 1
max = 6

roll = input("Do you want to play? ")

while roll == "yes" or roll == "y" or roll == "Yes" or roll == "Y":

    print("Now rolling both dice....")
    time.sleep(1)
    print("calculating....")
    time.sleep(3)

    print(random.randint(min, max))
    print(random.randint(min, max))

    if min == max: #This if condition does not work even if the dice match. Needs work.
        print("Amazing! You scored a double!")
    else: #This else condition works regardless of the outcome.
        print("Oof! Might wanna keep trying!")

    roll_again = input("Roll again? y/n: ")
    if roll_again == "n" or roll_again == "no" or roll_again == "No" or roll_again == "N":
        print("Goodbye!")
        break
    # else:
    #     print("Invalid answer")
    #     break

如果满足if条件,将显示一条消息,祝贺用户对双精度数字进行评分,例如1,1或6,6。如果骰子没有显示匹配的数字,则else条件将打印一条消息,告诉用户下次运气更好。

2 个答案:

答案 0 :(得分:0)

那是因为您预设了 min max ,它们将永远不会更改。因此, if min == max 不能为True。

您只需将结果保留在新变量中即可

dice_1 = random.randrange(1, 7)
dice_2 = random.randrange(1, 7)

然后比较dice_1和dice_2的条件

答案 1 :(得分:0)

尝试:-

import os
import datetime

directory = r'C:\Users\vasudeos\OneDrive\Desktop\Test Folder'
extensions = (['.jpg', '.jpeg', '.png']);
import random
import time

print("Welcome to the dice simulator, lets find out how lucky you are!")

"\n"

min = 1
max = 6

roll = input("Do you want to play? ")

while roll == "yes" or roll == "y" or roll == "Yes" or roll == "Y":

    print("Now rolling both dice....")
    time.sleep(1)
    print("calculating....")
    time.sleep(3)

    min1 = random.randint(min, max)
    max1 = random.randint(min, max)

    print("{}\n{}".format(min1, max1))


    if min1 == max1: #This if condition does not work even if the dice match. Needs work.
        print("Amazing! You scored a double!")
    else: #This else condition works regardless of the outcome.
        print("Oof! Might wanna keep trying!")

    roll_again = input("Roll again? y/n: ")
    if roll_again == "n" or roll_again == "no" or roll_again == "No" or roll_again == "N":
        print("Goodbye!")
        break

正如其他人指出的那样,变量minmax在程序执行期间具有恒定值(1、6),因为它们的值不相同,甚至不相同更改if min == max始终为假。