简体   繁体   English

循环python,返回输入函数

[英]loop python, return input function

I am trying to create a loop that will get me asking for a user's bet until it runs out of money or decides to quit.我正在尝试创建一个循环,让我要求用户下注,直到它用完钱或决定退出。 How can i take inputs from the different functions?我如何从不同的函数中获取输入?

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()

What you need to do is create a way to get input, return it, check if it is valid, then call the slot-machine function.您需要做的是创建一种方法来获取输入,返回它,检查它是否有效,然后调用老虎机函数。 Also, you can use input(s) ;此外,您可以使用input(s) ; s is the prompt you want to show ( s is usually a string). s是您要显示的提示( s通常是一个字符串)。 I suggest housing this all under a main() function:我建议把这一切都放在一个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()

I suggest you read about functions and return statements .我建议您阅读有关函数返回语句的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM