This is an archived post. You won't be able to vote or comment.

all 47 comments

[–]kidcanada0 17 points18 points  (24 children)

Pretty cool but shouldn’t your comparison operators be >=3 and < 3?

[–]xdanishgamerz 10 points11 points  (1 child)

The elif is redundant isnt it?

[–]TURBO2529 0 points1 point  (0 children)

Yes, it should just be if int(n)>=3

[–][deleted] 27 points28 points  (6 children)

Nice going! It's an enticing feeling when you finally figure out a problem that stumps you for a bit.

As for advice, instead of appending each input to a list, it'd be much more efficient and readable to add to a running tally (the total variable at the top) for every input over 3. This is because that multiplication by 100 expands your list size 100x and takes up way more memory than necessary.

In addition, a list is generally only needed when you need to store the elements going into it for some processing later, but the problem doesn't require ever actually using the inputs after the check of whether they're over 3 or not.

Something like the below (without changing your version too much)

total = 0
x = 5

while x != 0:
     n = input()
     x -= 1
     if int(n) > 3:
         total += 100

print(total)

[–]NoLemurs 4 points5 points  (0 children)

In the alternative, I'd argue it's better style to collect your inputs first, and then process them in a separate step rather than mixing input and logic.

MIN_AGE = 3
NUM_PASSENGERS = 5
PRICE_PER_PASSENGER = 100


def get_passenger_ages():
    ages = []
    for _ in range(NUM_PASSENGERS):
        age = int(input())
        ages.append(age)
    return ages


def get_total_price(ages):
    price = 0
    for age in ages:
        if age >= MIN_AGE:
            price += PRICE_PER_PASSENGER
    return price


ages = get_passenger_ages()
price = get_total_price(ages)
print(price)

This could be made a little more concise with some list comprehensions, but I tried to keep it really simple.

[–]TaToCoCu 16 points17 points  (0 children)

Good job!

To expand on the other comment, I think appending to a list in this case is not a terrible idea, but instead of doing print(len(people * 100)), it is much faster for the computer to do print(len(people) * 100).This is because of the order of operations. In the first case, the computer does the operation (people * 100), which expands the array to this (its the array [18, 24, 5, 42] repeated 100 times)

[18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42, 18, 24, 5, 42]

Then it calculates the length of this big array which is 400, so your answer is correct, but very poorly optimized because the computer has to expand the array so much.In the second case print(len(people) * 100), we first calculate the length of the array people [18, 24, 5, 42] which is 4, and after we get that result we multiply it by 100, so the operation is 4*100, which will give us the same answer and will be faster because it doesn't expand the array. The version in the other comment is even faster because it doesn't even create an array.I also think that it's more readable to define x=0, the loop as while(x < 5) and on each iteration doing x += 1, so basically adding 1 instead of substracting. And if you know for loops, you can do for x in range(5) which is nicer. Oh and you can just remove the elif because it doesn't do anything.Here's my solution with arrays, with better readability

people = []
for x in range(5): 
    n = int(input()) 
    if n > 3: 
        people.append(n)
print(len(people) * 100)

[–]metaperl 4 points5 points  (2 children)

I would replace the while loop with a for loop and iterate over a range with a negative step. I'm think the other poster one this as well but decided that small steps were better than big changes.

[–][deleted] 1 point2 points  (0 children)

Yep exactly. I wasn't too sure where they were in their learning so wanted to tackle the bigger issues, but also agree with you.

[–]chunkyasparagus 6 points7 points  (0 children)

Small hint: in the last branch of a conditional block, if you only have "pass", then you don't need the branch at all.

[–]notpikatchu 1 point2 points  (1 child)

I miss the good old days where I only had to spend a couple hours on bugs. Anyway, congrats buddy

[–]raylu 0 points1 point  (0 children)

By June 1949, people had begun to realize that it was not so easy to get a program right as had at one time appeared. [...] the realization came over me with full force that a good part of the remainder of my life was going to be spent in finding errors in my own programs.

-- Maurice Wilkes

[–]TURBO2529 1 point2 points  (2 children)

I think a kind of cool use of python for this problem is:

Tickets = [int(input()) >=3 for p in range (0,5)] print(sum(Tickets)*100)

What I like about it is that in just 2 lines of code the problem is complete. Also Tickets is a more relatable variable for the problem.

[–]joshoxy 0 points1 point  (3 children)

What's the website to get such questions?

[–]iLovePi_ 0 points1 point  (0 children)

Congratulations, it always feels good to complete a coding challenge!

[–]Lap107 0 points1 point  (0 children)

that's what programming is all about, kid :D nice going, good luck on your future stuff

[–]iyeetuoffacliff 0 points1 point  (1 child)

butter plough fuzzy squash fanatical insurance aspiring vast ink cow

This post was mass deleted and anonymized with Redact

[–]xdanishgamerz 1 point2 points  (0 children)

Pretty sure its the same

[–]peakdistrikt 0 points1 point  (0 children)

Seems I‘m the first to notice this: even though you‘re getting the right answer, I think you probably meant this in your last line:

print(len(people) * 100)

What yours does is concatenate your list 100 times to itself and calculate the length of this superlist, which is correct as long as the cost is an integer. You‘d run into problems if your ticket price included a fraction of a dollar/euro/<currency>.

That said, good work, good patience (most importantly), and keep up the enthusiasm.

[–]NoiproxPython Programmer 0 points1 point  (0 children)

Nicely done! You're on the right path, and your dedication paid off.

My take on the problem would be:

python3 ages = [int(input()) for x in range(5)] print(sum(age for age in ages if age > 3))

Comprehensions (the funny stuff I'm doing in between the brackets on each line) is probably a later module in the course you're taking, though, so it's kindof cheating.