georgeh1ll
Badges: 5
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 2 years ago
#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.
0
reply
winterscoming
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 2 years ago
#2
(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:
Code:
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?
Code:
if x<1 and x>0: print('Valid input')
Last edited by winterscoming; 2 years ago
0
reply
georgeh1ll
Badges: 5
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report Thread starter 2 years ago
#3
(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?
Code:
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.
0
reply
winterscoming
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report 2 years ago
#4
(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/video...ing-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).
0
reply
georgeh1ll
Badges: 5
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report Thread starter 2 years ago
#5
(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/video...ing-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
0
reply
winterscoming
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report 2 years ago
#6
(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.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Have you ever considered or are you currently considering an apprenticeship?

Yes, I am actively considering an apprenticeship (79)
13.57%
I am actively considering an alternative to uni that isn't an apprenticeship (11)
1.89%
I have considered an apprenticeship but it's not for me (147)
25.26%
I am considering a degree apprenticeship (47)
8.08%
I haven't considered an apprenticeship (280)
48.11%
Something else (let us know in the thread!) (18)
3.09%

Watched Threads

View All