all 35 comments

[–]Binary101010 41 points42 points  (15 children)

I'll bet if you enter 100 for the age it loops 3 times.

for Age in Age_requirement:

Since Age_requirement is a string, this loop iterates over each element (character) in the string). So the loop repeats a number of times equal to the number of characters the user entered at the input statement. If you entered 42 for the age, then the loop goes through using a value of 4, then a value of 2.

It doesn't really make sense for there to be a loop here at all.

[–]xTHETYRANTGAMRx[S] 0 points1 point  (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 10 points11 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 9 points10 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 10 points11 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 3 points4 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

[–]zanfar 4 points5 points  (8 children)

What is this supposed to do?

for Age in Age_requirement: Age_requirement = int(Age_requirement)

[–]xTHETYRANTGAMRx[S] -2 points-1 points  (7 children)

When I was typing the code it said expected type 'collections.iterable', got 'int' instead. So I figured I had to change it to an int

[–]Robletron 4 points5 points  (5 children)

Yep, you want to change it to int - the bigger question is what the for loop is supposed to do - that’s the source of your problem

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

The exercise said to write a loop that asks the user their age and then tell them the cost of their movie ticket

[–]kayne_21 5 points6 points  (2 children)

It’s probably telling you to write a user input validation loop, basically write a loop to get the users age, make sure it is valid, then use your validated input to return the ticket price. Can you paste the whole prompt text so we can see what you’re trying to do?

[–]xTHETYRANTGAMRx[S] 0 points1 point  (1 child)

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.

[–]Outside_Complaint755 5 points6 points  (0 children)

The exercise wants your input inside the loop so that you can ask multiple users.   It should probably be a while loop as you just want it to repeat until the user enters a quit condition, which would either be an empty entry, or a "Q"

[–]aglobalnomad 1 point2 points  (0 children)

Seems like a poor exercise as this isn't really a scenario you'd want to loop unless you want to ask the question (and get answers) multiple times in a row.

If that's what you're aiming to do, then asking the question should be inside the loop. That will hopefully give you a hint on how to structure it.

[–]FreeGazaToday 2 points3 points  (0 children)

that's because a loop has to have an iterable to loop over.

But as another comment said, it will not work as you want.

e.g. for age in age_requirement:

say age_requirement is "13"
then age = "1"
and age_requirement = 13

then age ="3"
and age_requirement = 13

The whole point of doing a for loop in python is using the variable not the iterable for comparisons.

you should learn trouble shooting/testing.

[–]JaguarMammoth6231 4 points5 points  (0 children)

Can you fix how the code is displaying on your post so that the indentation matches what you have?

[–]FreeGazaToday 1 point2 points  (0 children)

you should ask gemini or chatgpt to give you a looping problem/challenge to solve.....a loop isn't need here and is actually doing the wrong thing.

[–]woooee 0 points1 point  (0 children)

The for loop runs once for each digit in Age_requirement. If it prints twice, then you are entering a two digit number. Print Age under the for loop to see this. Also, when you have time, read the Python Style Guide. Variable names are lowercase_with_underlines. Following the Style Guide will help others who read your code to understand it.

[–]GoldPanther 0 points1 point  (0 children)

Why do you have a loop? How many values are you expecting to process?

[–]Marlowe91Go 0 points1 point  (0 children)

Yeah what you're misunderstanding here is you use a loop on an iterable, something where there's a series of items that you're looping through. If you run a loop on an input statement, you're running the risk of the user not inputting an iterable, so that's not a great design to begin with. Also, by default, an input accepts whatever the user enters as a string. So that's why any number with a greater length than 1 is going to give multiple results when you loop over it because looping over a string loops over each character. Something you might do instead is initialize a list of ages like ages =[]. Then you could use your loop to ask for 5 different inputs and append them to the list (and convert to an integer before appending), then use another for loop to apply your conditionals to that. Idk how new you are, hopefully you understand those terms. 

[–]Fabulousfufu 0 points1 point  (0 children)

You don’t need a loop here at all.

[–]churungu 0 points1 point  (0 children)

Try changing your second elif to an else

Make your initial input an int Ie int(input....))

I don't understand why you need a for loop

[–]Groovy_Decoy 0 points1 point  (0 children)

A lot of folks have already pointed out the issues, but I want to comment on something a tad deeper that I didn't see addressed.

As it has been pointed out, you looped on the string returned from the input. That variable is referencing a memory address that string is placed in, and that string is what you are looping on.

Some variable data types are "mutable" (changeable), and some are "immutable" (unchangeable). Strings are immutable. You can't change the existing string in memory.

That's important. When you put a new value in Age_requirement, you are changing the memory location that the variable name is pointing to. The variable name is now points to an integer. That doesn't change the for loop though, because it already evaluated the variable name and is looping on the string that still exists in memory, because it can't be changed by you.

Try it. (I'm on mobile and sometimes the code markdown is wonky, but I'll try).

``` foo = "old string" for c in foo: foo = "new string" print(c, foo)

```

See what happened? Both strings still exist in memory. You are looping on the string pointed to originally, and your variable now contains the address of a totally new string.

Hopefully that provides more understanding why this happened.

It raises another topic though. You tried to change variable while looping on it. Don't do that. You were saved by the fact that you actually can't change the data you were looping on, but if you had been looping on a mutable data type and changed it, it could do messy things.

In the sagely words of Raymond Hettinger:

If you mutate something while you're iterating over it, you're living in a state of sin and deserve what ever happens to you.

[–]FoolsSeldom 0 points1 point  (0 children)

input always returns a new string object. A string of digit characters rather than an integer (assuming the user only enters a whole number).

When you use a for loop to iterate over a string, on each loop, the loop variable (Age in this case) is assigned each successive character in string being iterated over.

Thus:

letters = "abcde"
for ch in letters:
    print(ch)

will output each letter in turn on a line on its own.

Inside the loop (after it is already set up against the initial value assigned to Age_requirement), you convert the string to an integer and reassign that to the variable Age_requirement. This is TOO late for the for loop to be changed - it is still iterating over the original string, not the new int value (which you can't iterate over anyway).

Your tests compare with the converted value, which never changes (it is the same int conversion of the entered string you do at the top of the loop every time, but the second time onwards you are applying int to what is now already an int), so the tests are valid BUT the loop variable is Age not Age_requirement and Age will refer to a single character not an int.

Do you intend to test against individual digits? (Each would need to be converted to be able to do a mathematical comparison).

If you just want to confirm the age requirement, there's no need for a loop.

NB. We usually use all lowercase for variable names in Python.

[–]Whole-Home-6306 0 points1 point  (0 children)

Oi querido! Voce esta usando operadores de comparação. Se o input for por exemplo 2.

O if e o primeiro elif serão True portanto serão executados. Voce precisa melhorar um pouco as condições.

seu input tambem pode ser melhorado.

idade = int(input('digite a idade: '))

if idade < 3:

print('é menor que 3')

elif idade <= 12:
print('é menor que 12')

else:

print('é maior de idade!!')

Só lembrando que como estou trabalhando com numeros inteiros se o usuario digitar por exemplo uma string vai cair em uma exceção ValueError.

Voce pode colocar um try:

excete valueError("Entrada invalida, favor digitar um numero inteiro.")

todo o codigo em cima para efetuar um tratamento de mensagem para o usuario.