我的程序应该如何运作?

时间:2016-02-17 00:45:14

标签: python

信息: 我正在开发一个我正在开发的新网站的程序,它将被称为“Coinflip”。在这个程序中,我试图使用所有必要的东西来确保获胜者得到公平的选择。这是公平的吗?我错了吗?因为我无法分辨,对于那些从未见过这个程序的人来说,可以告诉我哪里出错或是否正常工作会对我有所帮助。谢谢你的帮助!

代码:

import random

invinp = True

def main():

    winnerprob = []

    invinp = False
    user1 = input("User 1, Please input your name\n")
    user2 = input("User 2, Please input your name\n")

    winnerprob.append(user1)
    winnerprob.append(user2)

    random.shuffle(winnerprob)

    user1 = random.choice(winnerprob)
    user2 = random.choice(winnerprob)

    winner = random.randint(0,100)

    if winner <50:
        print(user1 + " wins!")
    elif winner >50:
        print(user2 + " wins!")

    print("When you enter your username, User 1 and User 2 are shuffled randomly to ensure that both parties have an equal chance of winning.")
    print("If unchanged, User 1's position was shuffled to: " + user1)
    print("If unchanged, User 2's position was shuffled to: " + user2)
    print("Here are the probabilities:")
    print("User 1 = 0, 50\nUser 2 = 51, 100")
    print("The number that was randomly generated was: " + str(winner))

while invinp:
    main()

1 个答案:

答案 0 :(得分:0)

您的代码大多数都很好,但它的工作量超出了需要。几乎像翻转硬币三次,以确保它是随机的。这就足够了:

import random

invinp = True

def main():

    winnerprob = []

    invinp = False
    user1 = input("User 1, Please input your name\n")
    user2 = input("User 2, Please input your name\n")

    winner = random.randint(1,100)

    if winner <=50:
        print(user1 + " wins!")
    elif winner >50:
        print(user2 + " wins!")

    print("When you enter your username, User 1 and User 2 are shuffled randomly to ensure that both parties have an equal chance of winning.")
    print("If unchanged, User 1's position was shuffled to: " + user1)
    print("If unchanged, User 2's position was shuffled to: " + user2)
    print("Here are the probabilities:")
    print("User 1 = 0, 50\nUser 2 = 51, 100")
    print("The number that was randomly generated was: " + str(winner))

while invinp:
    main()