What is the proper way to use continue? - python-2.7

I am running into problems accomplishing another iteration after using continue. The goal is to get an integer input that is greater than or equal to 3. I dont want the script to error out on the user, instead I would like to ask for another input.
while True:
try:
sides = int(raw_input("Marty wants to draw you a shape. How many sides will the shape have?"))
except ValueError:
print "Marty needs an integer, try again."
continue
if sides < 2:
print "Marty needs a number greater than 2, try again."
continue
else:
break
Does the issue come when using continue twice? Any advice in the proper use of continue would be great. As it stands now, it asks the user for an input. If given anything other than an integer, it asks for another input. If given 2 it does nothing, not even print, let alone try again for an input.

The problem isn't with your use of continue, it's with your evaluation of the input. Rather than what you have, try:
if sides <= 2:
print 'Marty needs a number greater than 2, try again.'
continue
or:
if sides < 3:

Related

Python 2.7 - If statement evaluates incorrectly when using .lower string modifier

New to Python and coding in general. I've tried to implement a simple code that will evaluate user input as either Y or N. If the answer is N, then the script should stop running. Otherwise, it should continue (in this case, printing a statement). Seems to work fine when I enter the exact string that is called for in the if statement. However, if I try to evaluate the answer using the .lower modifier, it just seems to ignore the input and proceed no matter what. Any assistance would be welcome. See code below. Thank you!
answer = str(raw_input("Enter Y or N: "))
if answer.lower == 'n':
quit()
else:
print "Cool! Next question."
You're not calling .lower, you're comparing the function .lower to the string 'n'. That will never be true.
Try this code snippet instead:
if answer.lower() == 'n':

Guessing game program with a while loop, where users enter a number and the computer tells them if it right, or to guess higher, or lower

My program continues to tell the user to guess lower, even if the user types in the correct number. How do I solve this?
import random
a=raw_input('enter a number')
b= random.randrange(0,11)
while a!=b:
if a < b:
print ('you are not correct, try a higher number')
if a > b:
print('you are not correct, try a lower number')
if a== b:
print('wwcd')
print b
The problems with it as currently are that a is never updated & that you are using 'a' the character rather than a the variable
while `a`!=b:
Compares a constant character to the number b (and will always be larger). It should be:
while a!=b:
This change should be applied to all your conditional statements (also it's probably best to remove the repeated if 'a'== b blocks, as only one is needed)
For the next part you need to update a as part of the loop (such that the user can change the input). You only need to move the part where you assign a a value downwards:
while a!=b:
a=raw_input('enter a number')
//rest of your conditionals statements
EDIT:
You have a 3rd problem. The raw_input() function returns a string and you need an int for comparison. To fix it simply cast it to int: int(raw_input('Enter a number')) or, more appropriately use Python 2.x's input() function. This will evaluate anything you input so will return int when you enter a number. But watch out, Python 3.x input() acts like raw_input() in 2.x and raw_input() is gone.

Appending to list from while loop Python 3

How can I append user int inputs to a list with a while loop? So whenever the number the user inputs is bigger than zero will be added to the list, but when it is a negative number the while loop will break and will continue to the next action. I am a total beginner in python 3, I tried a few things but didn't work. Here is what I tried :
numbers=[]
number = int(input("Please input a number: "))
while number>=0:
numbers.append(number)
if number <0:
break
You have two logical errors there:
First, you're never re-prompting for the number once you enter the while loop. You need to get a new number inside the loop so that you decide what to do upon the next iteration (append to the list, or stop the loop).
Second, your test if number < 0 is superfluous. Your loop runs only as long as number is greater or equal to zero; so inside the loop, there's no way the number can be smaller than zero. The test at the while above is quite sufficient.
Personally I'd rewrite the loop into an endless loop while True: ... and inside the loop I'd first prompt for a number. If that number were <0, I'd break out of the loop. Else, the remainder of the loop would be to append the new number to your list.
But there are countless solutions. Good luck!
You could use a for-loop instead like this pseudo code
if number<0
// do nothing or something or whatever
else:
yourRange = range(0,number)
for count in yourRange:
numbers.append(number)

User input ending loops on C++

I am trying to create something (I'm thinking probably a loop?) that will allow me to have the user enter several numbers and then enter something like "done" to get all the numbers added up.
for example if I have a float (call it x for now) they could enter "7 enter 5 enter 9 enter done enter" and it would add those numbers and make x that value. Where I am coming into problems is that I need the user to be able to be able to enter as many numbers as they want (between 1 and 70 as an example) without specifying how many numbers they want to enter, just entering something in when they are done.
Thank you all
You'll need to use an infinite loop (while (true) or for (;;)) to read the next input into a string.
Check if the string is done. If so, break the loop.
Then try to parse that string into a double (don't use float) with the function std::stod.
If the parsing fails, optionally print an error message like "Bad input, try again" and restart the loop. If the parsing succeeds, add the number to the counter and restart the loop.

Restrict users to enter numbers valid only till 2 decimal places C/C++

I am making an currency change program where I would be providing exact change to the input amount, for example a value of 23 would be one 20 dollars and 3 one dollar bills
I want to restrict the user to input the value only till 2 decimal places. For example: the valid inputs are
20, 20.4, 23.44 but an invalid input would be 20.523 or 20.000.
How can I do this is C/C++.
I read about one function that is setprecision but that is not what I want, setprecision allows to display the value till that decimal point, it still doesn't stop the user from entering any value.
Is there any way to do this?
Read the amount from the user as a string, either character by character or the entire line, and then check its format, and then convert it.
It's generally easier to let the user type whatever they want followed by the program rejecting the input if it isn't valid rather than restricting what they can type on a keystroke basis.
For keystroke analysis you would need a state machine with 4 states, which we can call Number, Numberdot, Numberdotone, and Numberdottwo. Your code would have to make the proper transitions for all keystrokes, including the arrow keys to move the cursor to some arbitrary place and the Backspace key. That's a lot of work.
With input validation, all you have to do is check the input using a regular expression, e.g. ^(([0-9]+) | ([0-9]+.[0-9]) | ([0-9]+.[0-9][0-9])$. This assumes that "20." is not valid. Then if it's invalid you tell the user and make them do it again.
I do not believe that there is any way to set the library to do this for you. Because of that you're going to have to do the work yourself.
There are may ways you can do this, but the only true way to handle restricting the input is to control reading it in yourself.
In this case you would loop on keyboard input, for ever keystroke you would have to decided if it can be accepted in the context of the past input, then display it. That is, if there is a decimal point you would only accept to more numbers. This also allows you to limit input to numbers and decimal places as well, not to mention input length.
The down side is you will have to handle all the editing commands. Even bare bones you would need to support delete and enter.
This is rather a task for the GUI you are using, than for core C/C++. Depending on your GUI/Web Toolkit you can give more or less detailed rules how data can or can not be entered.
If you are writing a normal GUI application you can control and modify the entered keys (in C or C++).
In a WEB application you can do similar things using javascript.
The best solution would be when all illegal input is impossible.