Python Python Basics All Together Now Gather Information

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
keith frazier
Python Development Techdegree Student 5,771 Points

Different approach to asking number of tickets, very weird result. I fixed it, but I don't understand what was wrong!

Instead of num_tickets etc. etc. I did:

ticket_amount = int(input("Hello {}, how many tickets would you like? ".format(name))) total_cost = TICKET_PRICE * ticket_amount print("{} tickets at ${} each will cost ${}".format(ticket_amount, TICKET_PRICE, total_cost))

When I entered 25 tickets in the console the result calculated was 25252525252525252525 (25 Repeated 10 times) It seems it multiplied the string "25" ten times. So I figured to make total_cost an int out the gate. I wrapped total_cost in an int like so: total_cost = int(TICKET_PRICE * ticket_amount) That didn't help.

Finally I wrapped the constant at the beginning of the script in an int like so: TICKET_PRICE = int(10) This was the solution, but wasn't TICKET_PRICE an int to begin with? Very confused...

Hey Keith, The First thing is to learn markdown basics, so you can have proper formatting, I think I might have understood the problem you are facing, but I need to see your code.

You can learn it here

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
keith frazier
Python Development Techdegree Student 5,771 Points

Instead of num_tickets etc. etc. I did:

ticket_amount = int(input("Hello {}, how many tickets would you like? ".format(name))) 
total_cost = TICKET_PRICE * ticket_amount print("{} tickets at ${} each will cost ${}".format(ticket_amount, TICKET_PRICE, total_cost))

When I entered 25 tickets in the console the result calculated was 25252525252525252525 (25 Repeated 10 times) It seems it multiplied the string "25" ten times. So I figured to make total_cost an int out the gate. I wrapped total_cost in an int like so:

total_cost = int(TICKET_PRICE * ticket_amount)

That didn't help. Finally I wrapped the constant at the beginning of the script in an int like so:

 TICKET_PRICE = int(10) 

This was the solution, but wasn't TICKET_PRICE an int to begin with? Very confused..

2 Answers

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
keith frazier
Python Development Techdegree Student 5,771 Points

Oops! I just didn't save it after I fixed the issue. Then I made another change that really did nothing and I thought it was the solution. I'm new to the online console­čśť, but thanks!

Ok, I think I identified your problem but your code provided here is still confusing and incomplete, anyways.

ticket_amount = int(input("Hello {}, how many tickets would you like? ".format(name))) 

total_cost = TICKET_PRICE * ticket_amount 

print("{} tickets at ${} each will cost ${}".format(ticket_amount, TICKET_PRICE, total_cost))

The only possible problem according to me is in ticket_amount.

ticket_amount = int(input("Hello {}, how many tickets would you like? ".format(name))) 

what I was confused with is that the ticket_amount provided by you is absolutely correct, there is no error in it but if your ticket_amount was missing that int before input then that was incorrect and would give the exact problem you are facing.

 ticket_amount = input("Hello {}, how many tickets would you like? ".format(name)

You said that you wrapped your total_cost and TICKET_PRICE with int so it solved your problem BUT actually that did not change anything and neither it solved your problem, I want you to try something right now, I want you to try this and tell me if it worked, the code below;

TICKET_PRICE = 10 #REMOVE THE INT because you don't need an int, TICKET_PRICE is already an int

ticket_amount = int(input("Hello {}, how many tickets would you like? ".format(name))) #Insert int here

total_cost = TICKET_PRICE * ticket_amount #remove int from here

print("{} tickets at ${} each will cost ${}".format(ticket_amount, TICKET_PRICE, total_cost))

I want you to try this code and I strongly believe you forgot to add an int to your ticket_amount. and that was the problem nothing else.


The question is WHY do we get "2525252525252552" 10 times?

Answer;

TICKET_PRICE = 10 

ticket_amount = input("Hello, how many tickets would you like?")  #REMOVED int here

total_cost = TICKET_PRICE * ticket_amount 

print("Total cost is {}".format(total_cost))


#Running this code in the console

# >>>  Hello, how many tickets would you like? 25

# >>> Total cost is 25252525252525252525

Now correction

TICKET_PRICE = 10 

ticket_amount = int(input("Hello, how many tickets would you like?"))  #INSERTED int here

total_cost = TICKET_PRICE * ticket_amount 

print("Total cost is {}".format(total_cost))

#Running this code in the console

# >>> Hello, how many tickets would you like? 25

# >>> Total cost is 250

The ticket_amount in the first example is taking the input as a str so what is actually happening is this;

total_cost = 10 * "25" #25 is a string here

#output >>> 25252525252525252525

In the second example

total_cost = 10 * 25 #25 is an int here

#output >>> 250

There is a function where you can check to see if an object is a str or an int.

print(isinstance(total_cost,str))

#output >>> False

the function isinstance takes basically 2 arguments, the first argument is to check if that belongs to the second argument, so in this case, I did, Does the total_cost belong to a string? and the program tells me "FALSE" meaning no. You should play around with your code ALOT to understand the nature of how python or programming works.