2017-03-29 19 views
0

我一直在撞牆撞了幾個小時,現在試圖想象這個所以任何幫助非常感謝。我想要做的是循環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! 
+0

您需要在第6行中引號。Y是變量「Y」是一個字符串。還有其他問題(你需要做一些事情或者只是通過第6行等)。但我想你是這樣問的。最後,請在下次您的問題中寫下您的代碼,以便重現。 – umutto

+0

只是把這個代碼,我壞我是新來的 –

回答

0

我想你應該說的第6行if yn == 'Y',而不是if yn == Y。你把Y當作一個變量,而它實際上是一個來自輸入的字符串。

0

要多次運行您的投幣模擬,您可以將其放入while循環中。

if yn == Y:測試將無法工作,因爲你還沒有定義的變量Y,所以當Python的嘗試執行該行你得到一個NameError。你實際應該做的是測試yn字符串'Y'的值。

我對你的代碼做了一些其他的小調整。我擺脫了潛在的危險eval函數調用,你不需要它。我也做了一個循環,要求所需的翻轉計數;當count是一個正數時,我們跳出循環。

import random 

def main(): 
    name = 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': 
     print("Ok have a nice day!") 
     return 

    while True: 
     heads = tails = 0 
     while True: 
      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") 
      else: 
       break 

     while tails + heads < count: 
      coin = random.randint(1, 2) 
      if coin ==1: 
       heads = heads + 1 
      else: 
       tails = tails + 1 

     print("you flipped", count , "time(s)") 
     print("you flipped heads", heads , "time(s)") 
     print("you flipped tails", tails , "time(s)") 

     yn = input("Would you like to run another coin toss simulation?(Y/N): ") 
     if yn != 'Y': 
      print("Ok have a nice day!") 
      return 

main() 

此代碼是爲Python 3.在Python 2裏,你應該在的地方input使用raw_input,你也應該把

未來進口print_function

在腳本的頂部,所以您可以獲得Python 3 print函數,而不是舊的Python 2語言版本。


對此代碼可以進行多項改進。特別是,它應該處理爲計數提供的非整數。如果輸入錯誤,該版本將會崩潰。要了解如何解決該問題,請參閱Asking the user for input until they give a valid response

相關問題