Repeat a set of code, until something is achieved - Python [duplicate] - python

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I have a program that gets a string from a user (raw_input) and sees if the length of it is greater than 8, for example. If it isn't it starts again, otherwise the program ends.
I'm using Python 2.7.8 and this is the code I'm using so far, but it doesn't work...
password = raw_input("Type in a password: ")
if password.len() > 8 :
print "Successful!"
else :
goto(1)
Can anyone solve this problem?

You need to learn the basics. You should start with a Python tutorial.
Here's the official Python tutorial from python.org; this section introduces the while statement, which you can use to make a loop.
https://docs.python.org/2/tutorial/introduction.html#first-steps-towards-programming

Related

New to Python, trying to do print statements. College textbook is super short not being helpful [duplicate]

This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")

Adding error check gets script stuck in infinate loop (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int

problem with multiple statements found while compiling a single statement [duplicate]

This question already has answers here:
How do I use raw_input in Python 3?
(9 answers)
SyntaxError: multiple statements found while compiling a single statement
(6 answers)
Closed 2 years ago.
x=int(raw_input("Enter first number"))
y=int(raw_input("Enter second number"))
I do not understand why I am unable to post this into my Python 3.8.2 shell? Does anyone have any advice on an alternative way?
Thanks.
raw_input is python2 syntax and you should use.input.
it was renamed in python 3, refer to the following link:
http://docs.python.org/dev/py3k/whatsnew/3.0.html
change to:
x=int(input("Enter first number"))
y=int(input("Enter second number"))
Regarding the issue of pasting both in the terminal, you should read about the input function and you'll see that once it's called it waits for an input and is something was pasted it will take it as an input.

Stop Python input when a specific character is entered [duplicate]

This question already has answers here:
How to read a single character from the user?
(26 answers)
Python sys.stdin.read(1) in a while(True) loop consistently executes 1 time getting input and multiple times not getting input
(2 answers)
Closed 6 years ago.
I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:
i = 0
array = []
while (i < 10):
array.append(raw_input("Prompt: "))
i = i + 1
However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.
I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.
Do you have any idea? Thanks in advance.
It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.
Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user
Then you need to process each character in a for loop.

Syntax error its there but where? [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
Once again Python's helpful error messages make me eat my keyboard.
I've checked the whole script forwards and backwards but can't find any "syntax error(s)".
Is there a decent debugger for Python or a helpful website which is able to scan my code for errors?
C:\Users\Daapii\Desktop>"foo.py"
File "C:\Users\Daapii\Desktop\foo.py", line 26
print "test"
^
SyntaxError: invalid syntax
Python why u no tell me more!
If this is Python 3, then the print function now requires parenthesis. Try print("test").
(EDIT: If Python continues to make you want to eat your keyboard, this might help.)
print is a function in Python 3:
print ("test")
Find other changes here: docs

Categories

Resources