+ 1

My python issues

EDIT: Ill change the thread to contain more questions if I run in to more issues, instead of creating a new thread each time :-) My initial question continue You are making a ticketing system. The price of a single ticket is $100. For children under 3 years old, the ticket is free. Your program needs to take the ages of 5 passengers as input and output the total price for their tickets. Sample Input 18 24 2 5 42 Sample Output 400 Ive tried 2 different things and nothings seems to work total = 0 ticket = 0 cal = int(input()) i = 0 while i < 6: cal = int(input()) if cal < 3: ticket = 0 else: ticket = 100 total += ticket i +=1 print(total) #for x in cal: # print(x) # cal = input() # cal = int(cal) # if cal < 3: # ticket = 0 # else: # ticket = 100 # total += ticket #print(total) thank you in advance for inputs

21st Feb 2021, 10:18 PM
Kristian Sørensen
Kristian Sørensen - avatar
21 Antworten
+ 4
I succeed with a different approach total = 0 #your code goes here ticket = 0 i = 0 while i < 5: x = int(input()) if x < 3: ticket = 0 elif x >= 3: ticket = 100 total += ticket i +=1 print(total) But can this solution be made with out defining that i has to be under 5
21st Feb 2021, 10:36 PM
Kristian Sørensen
Kristian Sørensen - avatar
+ 2
Rik Wittkopp No worries, been there done that a few times myself. 😁😁
21st Feb 2021, 11:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg its the python beginner course - > Control flow - > break & continue
21st Feb 2021, 11:25 PM
Kristian Sørensen
Kristian Sørensen - avatar
+ 2
Kristian Sørensen So, given the current lessons and stage at which this course is for this Pro code coach challenge, I believe the intent is to use the continue statement to skip over adding the $5 price of a ticket for those that are under 3 years of age. As for getting the inputs the way you are doing so is fine and correct for this stage in the lessons. You won't need the ticket variable when using the continue statement.
21st Feb 2021, 11:33 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Kristian Sørensen You really should have made a new thread in the Q&A for this, but anyway all you need to do in the calc(x) function is calculate the perimeter and the area and return the values as a tuple. p = side * 4 a = side * side return p, a
22nd Feb 2021, 2:32 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
That's because in your function you have x as the parameter not side which is what you are using and then you are setting the tuple to x within the function and not returning it. Change x to side (or use it correctly on the right of the = for the calculations in place of side) and make sure you return the tuple. Also, a square has 4 sides and your calculations are assigned to the wrong variables (swap them). See my small code example.
22nd Feb 2021, 11:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Allan QBit I didn't say it wouldn't run or give the correct output. The only reason it works is because 'side' is a global variable. Look at your calc() function and the parameter vs the variable used. The way you have it there is no point in even having the function.
6th Apr 2021, 3:57 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Allan QBit Let you check what out? Just change your calc() function so the in the body of the function side is x. Instead of using side globally. Then it will be correct.
6th Apr 2021, 4:22 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks ChaoticDawg I will pull the response as you are correct that I did not read through.
21st Feb 2021, 11:13 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Kristian Sørensen To answer the question "But can this solution be made with out defining that i has to be under 5", the answer is yes. You would need to initialize i=1 rather than i=0. For example, in your question the first code example is looping 6 times (0 - 5) but there are only 5 inputs. That is why <6 is an issue. But if you simply adjust i=1 the test of less than <6 is correct. Hope that helps.
22nd Feb 2021, 3:43 AM
Paul K Sadler
Paul K Sadler - avatar
+ 1
@ChaoticDawg Will create another one next time! Ive tried something like this but i get an error on line 8 "p,a = calc(side)" def calc(x): #your code goes here area = side*3 peri = side*side x = (peri, area) side = int(input()) p, a = calc(side) print("Perimeter: " + str(p)) print("Area: " + str(a))
22nd Feb 2021, 10:15 PM
Kristian Sørensen
Kristian Sørensen - avatar
+ 1
#should help here def calc(side): p=side*4 a=side*side return p,a side = int(input()) p, a = calc(side) print("Perimeter: " + str(p)) print("Area: " + str(a))
5th Apr 2021, 6:27 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
+ 1
Allan QBit Your answer is incorrect. Take another look at it. Besides, I'm pretty sure that the it has been resolved already.
5th Apr 2021, 8:23 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg try running it
6th Apr 2021, 3:43 AM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
+ 1
ChaoticDawg cool let me check it out
6th Apr 2021, 4:03 AM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
0
Rik Wittkopp you may want to re-read the question and see how the inputs are given. Kristian Sørensen it may help to add where this code coach is from (which tutorial) and its lesson number, so we can see what has been taught so far. So, we don't give you an initial answer that is beyond what you have had the opportunity to learn so far.
21st Feb 2021, 11:10 PM
ChaoticDawg
ChaoticDawg - avatar
0
Ive since progressed to the intermediate course and run in to a new problem: path python intermedia->collection types -> tuple unpacking -> pro code coach -> square up. Im given this: Tuple Unpacking Tuples can be used to output multiple values from a function. You need to make a function called calc(), that will take the side length of a square as its argument and return the perimeter and area using a tuple. The perimeter is the sum of all sides, while the area is the square of the side length. Sample Input: 3 Sample Output: Perimeter: 12 Area: 9 and Im given this code: def calc(x): #your code goes here side = int(input()) p, a = calc(side) print("Perimeter: " + str(p)) print("Area: " + str(a)) Im quite lost on how to approach this, my initial thought is to read up on classes and functions, otherwise I dont understand where to begin with this one, any help is appreciated.
22nd Feb 2021, 1:51 PM
Kristian Sørensen
Kristian Sørensen - avatar
0
i=1 f=0 while i<=5: i=i+1 age=int(input()) if age<3: continue f=f+100 print(f)
12th May 2021, 6:06 PM
Sakshi Upadhyay
0
total = 0 #your code goes here ticket = 0 limit=0 while limit <= 4: x = int(input()) if x < 3: total = total elif x >= 3: total += 100 limit +=1 print(total)
24th Aug 2021, 11:27 AM
Engin Baştürk
Engin Baştürk - avatar
0
def calc(x): #your code goes here p = x*4 a = x**2 return [p, a] side = int(input()) p, a = calc(side) print("Perimeter: " + str(p)) print("Area: " + str(a))
17th Feb 2022, 11:18 AM
Vitaliy Solomatin
Vitaliy  Solomatin - avatar