I need help in class with creating a loop

  1. Prompt the user for a number from 1 to 100. Using a while loop, if they entered an invalid number, tell them the number entered is invalid and then prompt them again for a number from 1 to 100. If they enter a valid number - thank them for their input.
  • Prompt the user for a number from 1 to 100
  • While that number is less than 1 or that number is more than 100
    • Output that they entered an invalid number
    • Prompt them again for a number from 1 to 100
  • Output to the user, thanking them for their input

Hey there, this looks like a class assignment, until you confirm otherwise, we cannot help you :smiley:

1 Like

What language is this?

2 Likes

ay, we can’t help in potential HW XD

1 Like

I won’t give him code, but I can point him where he needs to go to learn it

2 Likes

eh, true, ok, sure… fine

Can you please show me where to go to learn it?

2 Likes

I found this Documention page which looks good to learn:
https://python101.pythonlibrary.org/chapter5_loops.html

1 Like

Assuming this is python…

First you need to get a random number, you can do that via the random library using randint()documentation here: random — Generate pseudo-random numbers — Python 3.11.1 documentation

A while loop is pretty simple, it runs while the given statement is true, so you can just do while True to run forever. Use break to exit the loop.

To get input from the user use the built in function input() which returns a string. You will have to convert that string to an integer which can you do with int()

Use an if statement to check if the number the user inputted is equal to your random number. If it is, break the loop.

Outside the loop, you can thank them for being correct.

1 Like

Hmm? @AnthonyPetrillo never asked for a random number.

2 Likes