The Student Room Group

Python ask for a input between 0 and 1

I want to write a program that will ask the user to input a value between 0 and 1, and repeatedly asks if the value is not in that range.

So far I have written this:

x = float(input('Enter a value for x between 0 and 1: '))
if True: x<1 and x>0, print('Valid input')
elif x>1:
x = float(input('Enter a value for x between 0 and 1: '))
elif x<0:
x = float(input('Enter a value for x between 0 and 1: '))
else:
print('Invalid Input')

But this doesn't repeatedly ask if there's an incorrect input.

help much appreciated.
Original post by georgeh1ll
I want to write a program that will ask the user to input a value between 0 and 1, and repeatedly asks if the value is not in that range.

So far I have written this:

x = float(input('Enter a value for x between 0 and 1: '))
if True: x<1 and x>0, print('Valid input')
elif x>1:
x = float(input('Enter a value for x between 0 and 1: '))
elif x<0:
x = float(input('Enter a value for x between 0 and 1: '))
else:
print('Invalid Input')

But this doesn't repeatedly ask if there's an incorrect input.

help much appreciated.


Your second line begins with this:

if True:

The expression 'if True' is always true, so neither your 'elif' statements nor your else statement will ever run.

However, you have another expression after the colon ':' which is doing nothing, because it's not being evaluated as part of the "if" condition: x<1 and x>0 (The only thing which is being evaluated for 'if' is the 'True' value)

Perhaps you intended to write this instead?
if x<1 and x>0: print('Valid input')
(edited 4 years ago)
Reply 2
Original post by winterscoming
Your second line begins with this:

if True:

The expression 'if True' is always true, so neither your 'elif' statements nor your else statement will ever run.

However, you have another expression after the colon ':' which is doing nothing, because it's not being evaluated as part of the "if" condition: x<1 and x>0 (The only thing which is being evaluated for 'if' is the 'True' value)

Perhaps you intended to write this instead?
if x<1 and x>0: print('Valid input')

Okay thanks but say if I enter 1.2, the first time I get asked to reenter a value but if I the in 1.2 again the calculation runs. How do I get it so the calculation will only ever run when if x is between 0 and 1.
Original post by georgeh1ll
Okay thanks but say if I enter 1.2, the first time I get asked to reenter a value but if I the in 1.2 again the calculation runs. How do I get it so the calculation will only ever run when if x is between 0 and 1.

Yeah, 'if' isn't able to repeat, you'll need to use a loop instead:
https://www.py4e.com/lessons/loops
https://student.craigndave.org/videos/ocr-gcse-slr2-2-the-use-of-the-three-basic-programming-constructs


The 2 most common kinds of loops are:
"for" - Repeats a fixed, predetermined number of times. (i.e. "For-each number from (??) to (??), Repeat:")
"while" - Repeat if some expression is true. (a bit like saying "Repeat this as as long as <something> is True:" )

Although you need to think about the logic as well - your 'if' tells the user when the input is valid, but to repeat when the input is 'not valid' (i.e. the opposite of your if expression).
Reply 4
Original post by winterscoming
Yeah, 'if' isn't able to repeat, you'll need to use a loop instead:
https://www.py4e.com/lessons/loops
https://student.craigndave.org/videos/ocr-gcse-slr2-2-the-use-of-the-three-basic-programming-constructs


The 2 most common kinds of loops are:
"for" - Repeats a fixed, predetermined number of times. (i.e. "For-each number from (??) to (??), Repeat:")
"while" - Repeat if some expression is true. (a bit like saying "Repeat this as as long as <something> is True:" )

Although you need to think about the logic as well - your 'if' tells the user when the input is valid, but to repeat when the input is 'not valid' (i.e. the opposite of your if expression).

Ive attempted this using this code:
x = float(input('Enter a value for x between 0 and 1: '))
while 0<x<1: print('Valid Input')
else:
x = float(input('Enter a value for x between 0 and 1: '))


But I am still having the same problem any help much appreciated thanks
Original post by georgeh1ll
Ive attempted this using this code:
x = float(input('Enter a value for x between 0 and 1: '))
while 0<x<1: print('Valid Input')
else:
x = float(input('Enter a value for x between 0 and 1: '))


But I am still having the same problem any help much appreciated thanks


You're close, but think about the logic and switch it the opposite way around, your task is to "repeatedly ask while the number is not between 0 and 1"

However, your while statement reads "Repeat While the number IS between 0 and 1" -- "print valid input" (meaning it will repeatedly print "valid input" if the number is inbetween 0<x<1)

So the logic in your code is the opposite of the task -

Firstly, you want to repeat on a logical expression which has the opposite effect. Also, in most cases you'd usually use 'binary' logical expressions (meaning two operands, not three) and join those together using AND/OR... e.g. (a less than b OR y greater than z)

Secondly, the task is to repeatedly re-ask the question if the input is invalid.. at the moment the 'print("valid input")' is being repeated instead.

Quick Reply

Latest

Trending

Trending