Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
userChoice3 = int(input("What is the weight of the coin bag"))
                while userChoice3 not in range (100-1000):
                    print("You need to add more coins")
                    userChoice3 = int(input("What is the weight of the coin bag"))




I can't figure out how to get the program to not ask to re enter if a valid answer is entered. The program at the minute asks to re enter even if a value over 100.

What I have tried:

I have tried using putting while userChoice3 in range instead of not in range.
Posted
Updated 11-Jan-20 1:19am
v3
Comments
Richard MacCutchan 11-Jan-20 7:16am    
Hint: range (100-1000) is equivalent to range(-900) which does not make much sense. See also my answer below.

This is exactly the same issue as How do I stop the loop from being a continuous loop?[^]. And you already have the answer. Please do not repost the same question.

And if you are really struggling with the basics of Python, then go to The Python Tutorial — Python 3.7.6 documentation[^] and work through the tutorials a few times.
 
Share this answer
 
v2
Comments
Coding1014 11-Jan-20 7:15am    
its not the same because it isn't a continuous loop
Richard MacCutchan 11-Jan-20 7:18am    
It is pretty much the same. But in either case, you are not going to learn Python by posting questions like this. Do what I suggested above and work through the tutorials.
Coding1014 11-Jan-20 7:19am    
i know how to use python and learning from others helps me better than watching tutorials does.
OriginalGriff 11-Jan-20 7:26am    
So stop "watching tutorials" - they are generally produced by people who have no idea how to produce a video, and generally little or no mor knowledge of the subject than their audience.
Instead, go on a course, or get a good book - and read it cover to cover, doing all the exercises.

You don;t learn by looking at what people have done, you learn by doing, by using what you have been told. That is the basis of a skill - and they are all the same. Looking at examples doesn't tell you anything about why something is the way it is, what alternatives were considered, and why they weren't implemented. It just presents you with a "single solution" to a "single problem" - and you need skills to cope with solutions to generic classes of problems instead.

If you like, it's like being able to drive: slam it in gear and stamp on the "go" lever works, but it doesn't help you know what to do when you find the way blocked by a fallen tree. Or when you need to parallel park. The "driving skill" helps you to work round problems as you encounter them instead of having a single "driving solution" which only works under one set of circumstances.
Richard MacCutchan 11-Jan-20 7:41am    
I don't think so.
Check the definition of Range: Python range() Function Explained with Examples[^]
You need the two argument version. 100-1000 is -900, not a range.
Python
while userChoice3 not in range (100, 1000):
   print("You need to add more coins")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900