我一直在撞牆撞了幾個小時,現在試圖想象這個所以任何幫助非常感謝。我想要做的是循環Y輸入的Y/N問題的程序,特別是當輸入Y時,我希望它按照示例輸出中所示的方式做出反應。一旦整個折騰過程已經完成,無法弄清楚如何使一個蟒蛇硬幣折騰程序循環返回到某個點
這裏是我的代碼:
import random
def main():
name = eval(input("Hello user, please enter your name: "))
print("Hello", name ,"This program runs a coin toss simulation")
yn = input("Would you like to run the coin toss simulation?(Y/N):")
if yn == Y:
elif yn == N:
print("Ok have a nice day!")
heads = 0
tails = 0
count = tails + heads
count = int(input("Enter the amount of times you would like the coin to flip: "))
if count <= 0:
print("Silly rabbit, that won't work")
while tails + heads < count:
coin = random.randint(1, 2)
if coin ==1:
heads = heads + 1
elif coin == 2:
tails = tails + 1
print("you flipped", count , "time(s)")
print("you flipped heads", heads , "time(s)")
print("you flipped tails", tails , "time(s)")
main()
這裏是我要找的樣本輸出:
Hello user, please enter your name: Joe
Hello Joe this program runs a coin toss simulation
Would you like to run the coin toss simulation?(Y/N):Y
Enter the amount of times you would like the coin to flip:50
you flipped 50 times
you flipped heads 26 times
you flipped tails 24 times
Would you like to run another coin toss simulation?(Y/N):Y
Enter the amount of times you would like the coin to flip:100
you flipped 100 times
you flipped heads 55 times
you flipped tails 45 times
Would you like to run another coin toss simulation?(Y/N):N
Ok have a nice day!
您需要在第6行中引號。Y是變量「Y」是一個字符串。還有其他問題(你需要做一些事情或者只是通過第6行等)。但我想你是這樣問的。最後,請在下次您的問題中寫下您的代碼,以便重現。 – umutto
只是把這個代碼,我壞我是新來的 –