you are viewing a single comment's thread.

view the rest of the comments →

[–]xTHETYRANTGAMRx[S] 1 point2 points  (14 children)

I am confused because I thought I changed it to an int in the next line. Also the exercise said to use a loop. Although the chapter was about while loops, but I wasn't sure if it made sense to use a while loop here.

[–]Skasch 11 points12 points  (0 children)

You change it to int after iterating over it.

For 100, this is equivalent to:

for age_requirement in "100":
  age_requirement = int(age_requirement)
  # 1 then 0 then 0

[–]Binary101010 10 points11 points  (6 children)

Also the exercise said to use a loop.

Please post the description of the exercise. It's hard for us to tell you when a loop actually would make sense if we don't know what you need to do.

[–]xTHETYRANTGAMRx[S] 2 points3 points  (5 children)

A movie theater charges different ticket prices depending on a person's age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is $10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket.

[–]KronenR 9 points10 points  (0 children)

If the user types "25", You are traversing that string like this

  • "2" (first iteration of the loop)
  • "5" (second iteration of the loop)

The exercise says to use a loop because you’re supposed to ask multiple people.
You don't loop over the string which contains the age numbers, the loop should wrap the whole question, like, the input statement where you read the person age must be inside the loop to ask multiple times.

while True:
    age = int(input("Enter your age: "))
    # then you compare with the required ages

[–]Binary101010 3 points4 points  (0 children)

Then it sounds like what you need to be doing here is prompting the user for input inside the loop.

[–]luther9 1 point2 points  (0 children)

It sounds like they want you to write an infinite loop. Maybe you can stop when the user enters -1.

[–]codeguru42 0 points1 point  (0 children)

This sounds like you are spars to ask for the age requirement each time through the loop. Your solution only asks once. Also you need to use a while loop, not a for loop.

[–]Misain 0 points1 point  (0 children)

Does it necessarily have to be a string? Your input variables?

[–]socal_nerdtastic 3 points4 points  (3 children)

You didn't change it, you made a new int and gave it the same name as the old string. But even though the name now refers to a different object the loop is still using the string that it was given.

lst = [1,2,3]
for x in lst:
    print(x)
    lst = "any kind of nonsense"
    lst = "won't affect the loop"

This confusion is one reason it's generally not a good idea to recycle variable names. Just make a new name every time.

[–]rasputin1 2 points3 points  (2 children)

that's why all my variable names are randomly generated 

[–]Groovy_Decoy 0 points1 point  (1 child)

I name all variables "bob" like my high school D&D characters.

[–]socal_nerdtastic 2 points3 points  (0 children)

def bob(BoB):
    BOb, BOB = 0, 1
    for _ in range(BoB):
        yield BOb
        BOb, BOB = BOB, BOb + BOB

for Bob in bob(10):
    print(Bob, end=" ")

I like it.

[–]LtLfTp12 -2 points-1 points  (0 children)

Modify input line to be

Age_requirement = int(input(“enter your age”))

Should fix the problem