Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

It gives complicated explanation for ValueError

I am currently doing my master ticket project. I got it to raise a ValueError if the amount of tickets requested is g greater than the amount tickets remaining. The error response for that is also suitable. However, when passing along a string, it gives the actual error instead of the simple, user-friendly output that I like. Can someone help? My code is as follows

TICKET_PRICE = 10
tickets_remaining = 100  
while tickets_remaining >=1:
      print("They're are {} tickets remaining".format(tickets_remaining))
      user_name =input("Hello, welcome to Ticket Master! What is your name? ")
      number_of_tickets= input( "Well...{}, how many tickets would you like to buy?".format(user_name))
      try:
        number_of_tickets = int(number_of_tickets)
        if number_of_tickets > tickets_remaining:
          raise ValueError("Sorry, we only have {} tickets left".format(tickets_remaining))
        if number_of_tickets == str:
          raise ValueError ("Incorrect value. Please write a number")
      except ValueError as err:
          print("We ran into an issue. {}. Please try again.".format(err))
      else:
          total= (number_of_tickets * TICKET_PRICE)   
          print(" {}, your total is {}".format(user_name,total))
          proceed= input(" Would you like to proceed with purchase, Y for yes, N, for no:  ")
          if proceed.lower() == "y":
              #TODO: Gather Credit Card Information and process it. 
            print("SOLD! Your transaction is approved.")
            tickets_remaining = (tickets_remaining - number_of_tickets)
          else:
            print("{}, thank you for your consideration. Come again!".format(user_name))

print("Sorry, they're are no more tickets")

1 Answer

Steven Parker
Steven Parker
229,787 Points

When you include the "err" value in the output, the technical system error message is being shown. But you can choose to display your own message and not include the system part if you want:

    print("We ran into an issue. {}. Please try again.".format(err))  # so instead of this
    print("Please try again and use only a numeric value.")           # you might use this

Steven Parker If I chose to make a customer response for the value error associated with a string will make the value error I raise for the number of tickets available more than remaining. I want them to be independent.

Steven Parker
Steven Parker
229,787 Points

Either way you do it, showing a message should not change the number of tickets. There must be some other issue.
Try making a snapshot of your workspace and post the link to it to allow accurate replication of the issue.