-1

お金がなくなるか、やめる決心をするまで、ユーザーの賭けを要求するループを作成しようとしています。さまざまな関数から入力を取得するにはどうすればよいですか?

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

1 に答える 1

0

あなたがする必要があるのは、入力を取得し、それを返し、それが有効であるかどうかをチェックし、そしてスロットマシン関数を呼び出す方法を作成することです。input(s)また、 ;を使用することもできます。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()

関数returnステートメントについて読むことをお勧めします。

于 2013-02-22T23:48:33.057 に答える