Python Python Basics All Together Now Handle Exceptions

Daron Anderson
Daron Anderson
1,968 Points

Can I receive help with errors?

import sys

Constant/ Ticket Price is 19 >> int

TICKET_PRICE = 10

declare & assign Ticket counter to int

tickets_remaining = 100

Run this code until there is no tickets remaining

while tickets_remaining >= 1: your_name = input("Hello, Welcome to MasterTicket!!, Your name is? ")

    # Output how many tickets are remaining using the ticket counter var
    print ("There are currently {} tickets left.".format(tickets_remaining))
    # Gather users name and assign it to a new variable
    # Error Handling Block VVVVVV
    # Expect a ValueError to occur , handle it appropriateky....test it
    try:
            buy_ticket = int(input("OK {}, How many tickets to buy? ".format(your_name)))

            #Raise Value Error for exceeded limit ticket purchase
            if buy_ticket > tickets_remaining:
                    raise ValueError("There are only {} tickets available.. Sowwy.".format(tickets_remaining))

    except ValueError as err:
    # Include err text int the output
            print ("Some thing is wrong {}".format(err))
    # End Error Handling ^^^^^

    # You want this code to run if you know how many tickets there are
    else:
            Result = buy_ticket * TICKET_PRICE
            print ("Your total will be ${}".format(Result))
            print ("Tell your friends before we run out!!")
            Cont = input("Would you like to continue {} ? Y/n ".format(your_name))

            if Cont.lower() == "y":
                    #TODO: Gather CreditCcard Info and process it
                    print ("Sold!!")
                    # Reduce amount of tickets remaining by the amount sold
                    tickets_remaining -= buy_ticket

            elif Cont.lower() == "n":
                    print ("Too bad? Maybe Later? Thanks anyways!!")
                    sys.exit(0)

            # End greeting by thanking by name
            print ("Thank you for shopping with us!")

Notify the user no more tickets are available

print ("Sorry the tickets are sold out.. :(")

### I get the errors printed twice any help???

ERRORS: OK Daron, How many tickets to buy? blue

OUTPUT:Some thing is wrong invalid literal for int() with base 10: 'blue'

I guess this ones kind of no ERROR: OK daron, How many tickets to buy? 24354

OUTPUT:Some thing is wrong There are only 100 tickets available.. Sowwy.

Daron Anderson
Daron Anderson
1,968 Points

I kind of wrote it different because of the pasusing. I didnt break anything did I?

Kayc Mendes
Kayc Mendes
4,254 Points

Hey Daron, I tried to fix your code without changing it too much so you can see what you might have missed, hope it helps you out.

# Output how many tickets are remaining using the ticket counter var

import sys

TICKET_PRICE = 10

tickets_remaining = 100

#You forgot to write your code inside the while function

while tickets_remaining >=1:

    print("There are currently {} tickets left.".format(tickets_remaining))

    your_name = input("Hey there welcome, what's your name?  ")

    # You forgot to input user's name ^^^^

    #You could've blocked the error message only using Try and Except

    try:
        buy_ticket = int(
            input("OK {}, How many tickets to buy? ".format(your_name)))

        if buy_ticket >= tickets_remaining:
            print("Oh no, we're out of stock ")
            sys.exit()

    #In case the user tries to buy more than our stock ^^

        elif buy_ticket <= 0:
            print("You need to insert more than 0 ")
            sys.exit()    

        #In case the user tries to insert a negative number ^^ 

    except ValueError as err:
        err = print("Something is wrong")
        sys.exit()

    #In case the user tries to type something else 

    result = buy_ticket * TICKET_PRICE

    # You could've also printed the results without using try or else

    print("Your total will be ${}".format(result))
    print("Tell your friends before we run out!!")
    Cont = input("Would you like to continue {} ? Y/n ".format(your_name))

    if Cont.lower() == "y":
        # TODO: Gather CreditCcard Info and process it
        print("Sold!!")
        # Reduce amount of tickets remaining by the amount sold
        tickets_remaining -= buy_ticket

    elif Cont.lower() == "n":
        print("Too bad? Maybe Later? Thanks anyways!!")
        sys.exit()

            # End greeting by thanking by name

print("Thank you for shopping with us!")

#Last line out of the while function so it prints it once the code is over

2 Answers

Daron Anderson
Daron Anderson
1,968 Points

@Kayc Mendez When you ran it did it loop through when thrown errors?

Kayc Mendes
Kayc Mendes
4,254 Points

This fixed one? no

Daron Anderson
Daron Anderson
1,968 Points

@kayc Mendez ok no prob I was writing it my own way when he was asking to pause the video, It was working and then I lost my mind somehow and yea.. Thanks!