循环python,返回输入函数
我试图创建一个循环,让我要求用户下注,直到它用完钱或决定退出。如何从不同的功能获取输入循环python,返回输入函数,python,python-3.x,Python,Python 3.x,我试图创建一个循环,让我要求用户下注,直到它用完钱或决定退出。如何从不同的功能获取输入 import random import sys def greeting(): print('COP1000 Slot Machine - Jesus Pacheco') print('Let\'s play slots!') # Function ask user starting amout to bet # then it will check for any input er
import random
import sys
def greeting():
print('COP1000 Slot Machine - Jesus Pacheco')
print('Let\'s play slots!')
# Function ask user starting amout to bet
# then it will check for any input error
def intro():
greeting()
print('How much money do you want to start with?')
print('Enter the starting amount of dollars.', end='')
total = int(input())
print('Your current total is '+ str(total)+'\n')
while True:
print('How much money do you want to bet (enter 0 to quit)?', end='');
# Bett will be the amount of money that the player will bet
bett = int(input())
if bett == 0:
sys.exit()
if bett > int(total):
print('ERROR You don\'t have that much left')
elif bett < int(0):
print('ERROR: Invalid bet amount\n')
else:
break
# Function will ask user to press enter to play slot machine
def slotMachine():
print('Press enter to pull slot machine handle!')
input()
# Function shows results of slot machine after handle being pulled
def random():
import random
num1 = random.randint(1, 5)
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
print('/---+---+---\ ')
print('|-'+ str (num1)+'-|-'+ str(num2) +'-|-'+ str (num3) +'-|')
print('\---+---+---/ ')
if num1 == num2 and num2 == num3:
print('JACKPOT! cha-ching, you win')
if num1 == num3 or num2 == num3:
print('Match two, you get your bet back!')
else:
print('sorry, no match')
intro()
slotMachine()
random()
input()
随机导入
导入系统
def问候语():
打印('COP1000老虎机-耶稣帕切科')
打印('让我们玩老虎机!')
#函数询问用户开始下注的金额
#然后它将检查输入错误
def intro():
问候语()
打印('你想从多少钱开始?')
打印('输入美元的起始金额',结束='')
总计=整数(输入()
打印('您当前的总数为'+str(总数)+'\n')
尽管如此:
打印('您想下注多少钱(输入0退出)?',结束='';
#Bett是玩家将下注的金额
bett=int(输入())
如果bett==0:
sys.exit()
如果bett>int(总计):
打印('错误您没有那么多剩余')
elif bett
您需要做的是创建一种获取输入的方法,返回它,检查它是否有效,然后调用老虎机函数。此外,您还可以使用输入
s
是要显示的提示(s
通常是字符串)。我建议将这一切都放在main()
函数下:
import sys
import random
def main():
intro()
total = int(input('Starting Money: '))
if total <= 0:
raise ValueError('Starting Money must be greater than 0.')
# the above raises an error if the bet is less than 0
while True:
bet = getInput(total)
total = slotMachine(total, bet)
if total == 'win' or total <= 0:
break
def intro():
print('COP1000 Slot Machine - Jesus Pacheco')
print("Let's play slots!")
def getInput(total):
userInput = int(input('Bet (0 to quit): '))
if userInput > total or userInput < 0:
raise ValueError('Bet must be less than total and greater than 0.')
if userInput== 0:
sys.exit()
# the above raises an error if the bet is less than 0 or greater than the total
return userInput
def slotMachine(total, bet):
num1 = random.randint(1, 5)
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
print('/---+---+---\ ')
print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|')
print('\---+---+---/ ')
if num1 == num2 and num2 == num3:
print('JACKPOT! Cha-Ching, you win!')
return 'win'
elif num1 == num3 or num2 == num3:
print('Match two, you get your bet back!')
return total
else:
print('Sorry, no match.')
return total - bet
main()
导入系统
随机输入
def main():
简介()
总计=整数(输入('起始货币:'))
顺便说一句,如果是总计,当您检查“第二场比赛”时,您应该使用
elif
(else if)而不是if
,因为现在如果用户赢得了头奖,他们也将赢得第二场比赛(因为或不是排他性的:True或True
具有值True
)。如果总数='win'或总数<0,则不应为如果总数='win'或总数