python编写程序模拟硬币的投掷、假设0表示硬币的反面_修改了Python中的硬币投掷程序,无法完成循环...
謝謝你們的幫助!以下是完整的代碼:D'''
_MBE_
CIS-115-09
Lab 6-1
Write a python program, using modules / functions, to simulate flipping
a coin using a random number generator.
If the random number generator returns a 0 consider that a “tails”,
and a return of a 1 a “heads”.
At the beginning of the program ask the user how many times to flip the coin,
keep track of the total “heads” and “tails” and print the results after
the coin has been flipped the requested number of times.
Allow the user to enter another number of times to flip the coin
and re-run the program.
An input of zero (0) times to flip the coin will terminate the program.
'''
print; printHeader = raw_input("Please enter your name: ")
print; print printHeader, "| Lab 6-1"; print
raw_input("Press [ENTER] to continue...."); print
import random # importing the random library to allow for use of random
#random functions later on
# declaring the variable to take input on how many times to flip the coin
timesToFlip = int(input("Enter the number of times to flip the coin: ")); print
# defining the function for the coin flip game
def coinFlipGame(timesToFlip):
coinHeads = 0 # used to store how many times the coin is heads
coinTails = 0 # used to store how many times the coin is tails
accumulator = 0 # ensures that the coin is only flipped a certain number of times
while accumulator < timesToFlip:
coinFlip = random.randint(0,1)
if coinFlip == 1:
accumulator += 1
coinHeads += 1
print "After", accumulator, "flip(s) of the coin, the result was heads!"
raw_input("Press [ENTER] to continue...."); print
else:
accumulator += 1
coinTails += 1
print "After", accumulator, "flip(s) of the coin, the result was tails!"
raw_input("Press [ENTER] to continue...."); print
print "Flips:", accumulator, "| Heads:", coinHeads, "| Tails:", coinTails
while timesToFlip: # a loop to allow the program to keep running until
#an appropriate kill code is entered
coinFlipGame(timesToFlip)
timesToFlip = input("Enter the number of times to flip the coin: "); print
if timesToFlip == 0:
print "You have ended the game. Goodbye!"; print
raw_input("Press [ENTER] to end program....")
# end program
總結
以上是生活随笔為你收集整理的python编写程序模拟硬币的投掷、假设0表示硬币的反面_修改了Python中的硬币投掷程序,无法完成循环...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Java代码在ElasticSearc
- 下一篇: java文件名特殊字符_Java 8:用