Posts: 4
Threads: 1
Joined: Jan 2021
Jan-19-2021, 02:56 AM
(This post was last modified: Jan-19-2021, 03:46 PM by StingingNeedles.)
I am new to python and I am trying some practice problems and I can get this one to work:
-Prompt user for an integer between 1 and 10 and re-prompt if the input is not in this range
-Print that many "&", one per line
I know I need to use while True to make this work, but I can get it to loop more than a single &.
Any help would be appreciated, thanks.
Posts: 6,806
Threads: 20
Joined: Feb 2020
"while True:" is going to run over and over and over. The tricky part is getting it to stop.
One way to break out of the loop is to "break" out of the loop.
import random
while True:
value = random.randint(0, 10)
if value == 5:
break
print(value) This loop will print random numbers in the range 0..10 until the random number is 5. It then exits the loop without printing 5.
Inside a function you have an additional option; return. This loop counts how many random numbers are generated before the random number == value and returns the count.
import random
def counter(value, max_=10):
counter = 1
while True:
counter += 1
if random.randint(0, max_) == value:
return counter
print(counter(5, 11)) You'll have to decide what get placed inside the loop and how to exit the loop.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jan-19-2021, 07:32 AM
(This post was last modified: Jan-19-2021, 07:33 AM by perfringo.)
Modern Python has walrus operator so we can replace while True: with this:
while (num := int(input('Enter number: '))) != 5:
print(f'Number must be 5 but you entered {num!r}') Note that this crashes if user enters something which can't be converted into int ; it can be written without walrus as well but then there is no access/reference to the value user entered.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 4
Threads: 1
Joined: Jan 2021
Jan-19-2021, 04:21 PM
(This post was last modified: Jan-19-2021, 04:21 PM by StingingNeedles.)
i = int(input('Integer Between 1 & 10: '))
while True:
if i == 1:
print('&', end='\n')
elif i == 2:
print('&',end='\n')
print('&',end='\n')
elif i == 3:
print('&',end='\n')
print('&',end='\n')
print('&',end='\n')
elif i == 4:
print('&',end='\n')
print('&',end='\n')
print('&',end='\n')
print('&',end='\n')
This is the road I am going down, but I know there is a better way to loop this. Also, I need to figure out how to re-prompt the first line if they enter a number that is not between 1-10.
Posts: 6,806
Threads: 20
Joined: Feb 2020
Jan-19-2021, 04:40 PM
(This post was last modified: Jan-19-2021, 04:40 PM by deanhystad.)
You have the while loop in the wrong place. You are supposed to prompt the user for input until then enter a number between 1 and 10. You politely request them to do so, but nothing in you program enforces the thing they enter is a number, much less a number from 1..10.
Your program should look something like this:
while True:
num = get input
if num in range 1 to 10
break
print('&'*num) Now go figure out these parts:
num = get input # <- You have a good start, but what if user enters 'one'?
if num in range 1 to 10 # <- You have not done anything to test this
Posts: 4
Threads: 1
Joined: Jan 2021
(Jan-19-2021, 04:40 PM)deanhystad Wrote: You have the while loop in the wrong place. You are supposed to prompt the user for input until then enter a number between 1 and 10. You politely request them to do so, but nothing in you program enforces the thing they enter is a number, much less a number from 1..10.
Your program should look something like this:
while True:
num = get input
if num in range 1 to 10
break
print('&'*num) Now go figure out these parts:
num = get input # <- You have a good start, but what if user enters 'one'?
if num in range 1 to 10 # <- You have not done anything to test this
Okay, thanks for the help. This is making more sense now. How do I loop the "&"? For example if the person enters 4, I want to print:
&
&
&
&
Posts: 6,806
Threads: 20
Joined: Feb 2020
Posts: 4
Threads: 1
Joined: Jan 2021
(Jan-19-2021, 10:29 PM)deanhystad Wrote: Look at for loop.
while True:
i = int(input('Integer Between 1 & 10: '))
if i in range (1,11):
break
for i in range (1,11):
print('&', end='\n')
Here is where I am at now. It it re-promting correctly if a value is entered that is not 1-10, but when 1-10 is entered it is not printing anything. I know I am missing the second half of the For Loop, but I can't figure out what it is.
Posts: 6,806
Threads: 20
Joined: Feb 2020
Please use python tags when posting code.
This does not do what you think it does:
if i in range (1,11): and why are you doing this?
for i in range (1,11): I think being near a keyboard is messing you up. Put away the code for a while and do a little research. Formulate an algorithm to solve your problem. Write it down! Then, and only then begin work to convert you algorithm to Python code.
|