我是python的新手,无法让我的 def simspin 在主要版本中正确打印。当我使用手动数据输入在shell中运行该函数时,它返回正确的数据,但是当它通过main函数打印时,它不会打印完整的数据。在 def simspin 中返回是否可以做些不同的事情?
from random import randrange
from random import random
def printintro(): **Removed for Purposes of this post**
def inputvalues():
bankroll=int(input("Enter the number of dollars you start with:"))
spins=int(input("Enter the number of spins you will play:"))
bet=int(input("Enter how many dollars you will bet for each spin:"))
print()
return bankroll, spins, bet
def betstyle():.....**Removed for purposes of this post.**
def simspin(spins,placebet,bankroll,strategy,bet):
wins=0
losses=0
spincount=0
endingbank=bankroll
netwinnings=0
while spincount != spins and endingbank >0:
spincount=spincount+1
winningnumber=7
if strategy == 's' and placebet == winningnumber:
wins=wins+1
endingbank=endingbank+(bet*35)
netwinnings=netwinnings+bet*35
elif strategy == 'e' and placebet== winningnumber:
wins=wins+1
endingbank=endingbank+bet+bet
netwinnings=netwinnings+bet+bet
elif strategy == 'd' and placebet== winningnumber:
wins=wins+1
endingbank=endingbank+bet+(bet*2)
netwinnings=netwinnings+bet+(bet*2)
else:
losses=losses+1
endingbank=endingbank-bet
return wins,losses,spincount,endingbank,netwinnings
def printResults(spincount, wins, losses, endingbank, netwinnings):
winsperc=wins/spincount
lossperc=losses/spincount
print()
print("After {0} spins".format(spincount))
print("Wins: {0} ({1}%)".format(wins,100*winsperc))
print("Losses: {0} ({1}%)".format(losses,100*lossperc))
print("Ending bank: ${0}".format(endingbank))
print("Net winnings: ${0}".format(netwinnings))
def main():
bankroll, spins, bet=inputvalues()
printintro()
strategy=placebet=betstyle()
wins, losses, spincount, endingbank, netwinnings=simspin(spins,placebet,bankroll,strategy,bet)
printResults(spincount,wins,losses,endingbank,netwinnings)
main()
该程序目前打印以下内容......
Enter the number of dollars you start with:1000
Enter the number of spins you will play:1000
Enter how many dollars you will bet for each spin:1
Choose one of the following stragey choices
-Bet on a (s)ingle number (pays 35 to 1)
-Bet on a (e)ven or odd numbers (pays 1 to 1
-Bet on a (d)ozen numbers (pays 2 to 1
Enter your strategy choice (s,e,d):s
Enter the single number you want to bet on (00 or 0 to 36): 7
**After 1000 spins
Wins: 0 (0.0%)
Losses: 1000 (100.0%)
Ending bank: $0
Net winnings: $0**
我想要打印...
Enter the number of dollars you start with:1000
Enter the number of spins you will play:1000
Enter how many dollars you will bet for each spin:1
Choose one of the following stragey choices
-Bet on a (s)ingle number (pays 35 to 1)
-Bet on a (e)ven or odd numbers (pays 1 to 1
-Bet on a (d)ozen numbers (pays 2 to 1
Enter your strategy choice (s,e,d):s
Enter the single number you want to bet on (00 or 0 to 36): 7
**After 1000 spins
Wins: 1000 (1000.0%)
Losses: 0 (0.0%)
Ending bank: $36000
Net winnings: $35000**