Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Its been a while since I've written code, so I'm trying to get back into it again, but I'm having trouble with some of my code.
I wanted to write a simple program that takes a user's input and checks to see if it is all letters with no spaces, and has a length less than 12. I keep getting an "Invalid syntax" error on line 17 whenever I run the code, pointing to the colon after the if statement that checks if the username is just letters and less than 12 characters. I know that means there's an error on the line before that, but where?
#import the os module
import os
#Declare Message
print "Welcome to Userspace - Your One-Stop Destination to Greatness!" + "\n" + "Please enter your username below." \
+ "\n" + "\n" + "Username must be at least 12 characters long, with no spaces or symbols." + "\n" + "\n"
#uinput stands for user's input
uinput = raw_input("Enter a Username: ")
#check_valid checks to see if arguement meets requirements
def check_valid(usrnameinput):
if (usrnameinput != usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
os.system('cls')
print "Invalid Username"
return False
else:
os.system('cls')
print "Welcome, %s!" % (usrnameinput)
return True
#Asks for username and checks if its valid
print uinput
check_valid(uinput)
#Checks input to check_valid is correct, and if it is not, then ask for new username input and checks it again
while check_valid(uinput):
return True
break
else:
print uinput
check_valid(uinput)
print "We hope you enjoy your stay here at Userspace!"
UPDATE - I played around with the code a little bit more, and the only thing I changed was the while conditional to an if:
print uinput
check_valid(uinput)
#Checks input to check_valid is correct, and if it is not, then ask for new username input and checks it again
if check_valid(uinput):
print uinput
check_valid(uinput)
print "We hope you enjoy your stay here at Userspace!"
I ran this code, but got this error instead:
File "Refresher In Python.py", line 39
return True
SyntaxError: 'return' outside function
Sorry for being such a noob. Also just joined Stack Overflow today.
I believe this is what you want. I suggest splitting it into two functions, your check_valid() and a general main() function.
def check_valid(usrnameinput):
if (not usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
print("Invalid name")
return False
else:
print("Welcome!")
return True
def main():
uinput = raw_input("Enter a Username: ")
while not check_valid(uinput): #Repeatedly asks user for name.
uinput = raw_input("Enter a Username: ")
main()
Related
This question already has answers here:
String contains any character in group?
(5 answers)
Closed last year.
I want to check if username has any Character from blockCharacter but i could'nt do it and this is my code
user = input("Whats your name\n").lower()
user = (user.title())
BlockChar = ["+","none","-"]
if user == BlockChar:
print("That does'nt not feels right. Try Again")
breakpoint
print ("Welcome " + user)
I am new to codeing and stack overflow so i need some help with it
The any function and a generator expression will work quite nicely to determine if any of the strings in BlockChar are in your user string.
if any(ch in user for ch in BlockChar):
...
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
i'm absolutley garbage at python 3. this may not be a hard question but ive tried for hours to create a solution to this question and cant make a functioning code. i've used while and if statements.
"As the user enters passenger details such as e-ticket number, full name, and destination, the program validates the user input. If an invalid input is entered,the program displays an appropriate error message and asks the user to re-enter invalid values until each entry is correct"
this is very easy
helle.py
while True:
inputdata = input()
if inputdata != "valid":
print("not valid")
else:
print("valid")
break
I have a login system in which students take quizzes at different difficulties. what i need to do is load the question and answers for the quiz from an external .txt file. can someone help me quickly please as i need to have this done very soon. can the coding be simple aswell and available to use on python 3.4 as i am not very good with python
This is my attempt at the code:
def easyMathsQuiz():
score=0
emquiz=open("easymathsquiz.txt","r")
questionNumber=0
for questionNumber,line in enumerate(emquiz):
print (line)
ans=input("Your answer is: ")
if ans == "1":
score=score+1
questionNumber=questionNumber+1
elif ans=="2":
questionNumber=questionNumber+1
elif ans !="1" or ans !="2":
print("You have entered an invalid character")
easyMathsQuiz()
break
for questionNumber,line in enumerate(emquiz):
print(line)
if ans == "2":
score=score+1
questionNumber=questionNumber+1
elif ans=="1":
questionNumber=questionNumber+1
elif ans !="1" or ans !="2":
print("You have entered an invalid character")
easyMathsQuiz()
easyMathsQuiz()
print (score)
This is whats inside the .txt file:
What is 2+2-1? 1)3 2)4
What is 10+10? 1)30 2)20
What is 3*9? 1)27 2)36
What is 100/5? 1)25 2)20
What is 30-17? 1)23 2)13
My problem is:
Each line number basically represnts the question number. the first line i got to print but im just not sure how to make the next line print and i need the system to allow the user to input and of course it needs to check if their answer is correct. And i also have just absolutely no idea how to write code for it to go to the start of the question when they enter an invalid character rather than make the whole thing restart
Btw i cant seem to get the code to indent properly on here everything is indented correctly on my program
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How can we store the output of 'type' function in a variable ?
next = raw_input('> ')
type = type(next)
if type == 'int':
val = int(next)
else:
print "Type a number!"
Syntax Error at line 4....?
There are several ways of doing what you want. Note that defining a type variable to mask the type function is not good practice! (and next either BTW :))
n = raw_input('> ') # (or input in python 3)
try:
val = int(n)
except ValueError:
print("Type a number!")
or
n = raw_input('> ') # (or input in python 3)
if n.isdigit():
val = int(n)
else:
print("Type a number!")
Note: as some comment indicated, that in python 2, it was possible to get what you wanted by just using
n = input("> ")
but very ill adviced since you have to control what n is really, not python 3 portable, and has huge security issues:
Ex: in python 2 on windows, try that:
import os
n = input("> ")
and type os.system("notepad")
you'll get a nice notepad windows !! You see that it is really not recommended to use input (imagine I type os.system("del <root of your system>")) ...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've started to learn python only a day ago, seems to be easy, but still have some questions :)
how to write a script which have to check necessary input of raw_input?
I mean, the script must stop if a user have not entered any case and just hit enter on raw_input...
ans = raw_input('Enter: ')
if not ans:
print "You entered nothing!"
else:
print "You entered something!"
If the user hits enter, ans will be ''. and '' is considered False, thus as the condition is True (not False), the if block will run.
If you wish to continually ask the user for an input, you can use a while-loop:
ans = ''
while not ans: # While the input given is an empty string
ans = raw_input('Enter: ')
raw_input() return empty string ('') if user just hit enter.
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
if raw_input() == '':
break # or return
if not raw_input():
break # or return