-2

I'm having difficulty understanding loops.

Heres my problem.

Write an expression that executes the loop body as long as the user enters a non-negative number.

user_num =9;
while '"Your solution goes here"':
   print('body')
   user_num = int(input))

print ('Done.')

Can anyone help me with this and explain how loops work?

Joey
  • 1
  • 1
  • 5
  • did you defined input anywhere else? It is an unbound variable here – xiaodelaoshi Jul 23 '19 at 03:09
  • What is a "non-native number"? Something like 四, or ٤? But those too are native to someone... (indeed, quite a number of someones) – Amadan Jul 23 '19 at 03:09
  • `user_num` is 9 before loop which is a *"native"* number, but you want keep looping if it's a non-native. So the loop does not execute. – Austin Jul 23 '19 at 03:11
  • 1
    @XinlinFeng: `input` is not an unbound variable; it's bound to a native (!) function. By the paren imbalance (and sensible semantics), OP is probably trying to say `int(input())`. – Amadan Jul 23 '19 at 03:11
  • The answer is: while user_num >= 0. I'm just still having difficulty understanding loops – Joey Jul 23 '19 at 03:14
  • 1
    See: [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/364696); essentially the same problem (where "valid" means "negative" in this case). – ShadowRanger Jul 23 '19 at 03:16
  • It means exactly what it says, in plain English: "*while* the value `user_num` is greater than or equal to zero, do these things:". The only real caveat is that you have to get all the way through "these things" before the condition is checked again. – Karl Knechtel Jul 23 '19 at 03:17
  • @KarlKnechtel okay when it gets more complicated I have issues. Ex. Write a while loop that prints user_num divided by 2 until user_num is less than 1 – Joey Jul 23 '19 at 03:20
  • @Joey you wont learn by too much by having someone just tell you the answer. The best way is to read a substantial explanation of the subject (which a stack overflow answer almost never is). One popular book is learn python the hard way (while loop section [here](https://books.google.com.au/books?id=CfcEAQAAQBAJ&pg=PA110&lpg=PA110&ots=6WlTqe3tJs&focus=viewport&dq=python+hardway+while+loop)). – Paul Rooney Jul 23 '19 at 03:25

1 Answers1

0

Here's a solution with some comments on each line:

user_num =9; #Initialize a random non-native number to be able to go to the core of the while loop

while (user_num >= 0): #After each user input, check if the input is a non-negative number
  #If user input is a non-negative number, you end up here, inside the while loop
  print("Body") #This is one of the statements. It prints "Body"
  user_num = int(input())  #Acquire a new input from the user and store it in the variable user_num. At this point, the loop runs again using the new user input.
#If user input doesn't meet the while loop's condition, you end up here, outside the while loop
print('Done.')

The first line, user_num = 9, acts as the user's first input. You want this number to satisfy the while loop's condition, so that it executes the statements inside of it.

Here's a good place to begin with while loops: https://www.w3schools.com/python/python_while_loops.asp

Honey Gourami
  • 150
  • 11
  • @Muon no worries. Programming is fairly difficult to learn with no teacher or tutor with no background in the field. I'm just taking online classes and any help I can get is really appreciated – Joey Jul 23 '19 at 03:41