Usage of raw_input in python - python

I am having so much trouble with this python script:
import time
print "Loading Interface"
time.sleep(0.5)
print "Loaded Interface"
time.sleep(1)
question_one = raw_input = "Request: Enter your name: "
question_two = raw_input = "Request: Enter your password: "
time.sleep(1)
print "Searching for %s with the password %s in our database." % (question_one, question_two)
Could anyone tell me what I am doing wrong?

raw_input is a function, so you have to call it and not just assign some value to it.
Try this:
question_one = raw_input("Request: Enter your name: ")

for one, its not clear what you're asking. also, you have the program delay on purpose when it's not really loading... not sure why you would want to do that. and you're not actually calling raw_input, you're just assigning a variable to it. instead, try this:
question_one=raw_input("Request: enter your name: ")
this ^^^ will actually ask the user a question.

I believe you mean that the problem is that the raw import prompt does not work. This is because raw_import is a function, and the (optional) argument can be the import prompt (see: https://docs.python.org/2/library/functions.html#raw_input)
i.e. this should work:
import time
print "Loading Interface"
time.sleep(0.5)
print "Loaded Interface"
time.sleep(1)
question_one = raw_input ("Request: Enter your name: ")
question_two = raw_input ("Request: Enter your password: ")
time.sleep(1)
print "Searching for %s with the password %s in our database." % (question_one, question_two)

Related

How to make an abbreviation for line of code?

I want to put a pause in between almost every print statement, but I don't want to manually put
time.sleep(3)
in between each print.
import random
import time
print ("Hello")
time.sleep(3)
print ("What is your name?")
username = input("Name: ")
print ("Hello " + username)
time.sleep(3)
print("What brings you here?")
is there a way I can shorten it to a single word to put in between each thing?
You could write a function...
def print_and_sleep(message1, message2, delay):
print(message1)
time.sleep(delay)
print(message2)

How to make the code pause until the user clicks the enter key?

I have the following code, and i want the code to pause after each print statement until the user presses the enter key. How would I go about that?
import random
import time
print ("Hello")
print ("What is your name?")
lower_username = str(input("Enter Username: "))
upper_username = lower_username.capitalize()
print ("Hello " + upper_username)
Thanks for the help!
by making all of your print statements input() statements, they will only continue after a user intervention, e.g. an enter keystroke.
import random
import time
print "Hello"
input("What is your name?")
lower_username = str(input("Enter Username: "))
upper_username = lower_username.capitalize()
print "Hello " + upper_username
put input() at the end of code

How do I use %s multiple times

import time
import sys
print ("Welcome to the annoying machine. Made by Bloody")
time.sleep(2)
print ("Here you will enjoy this annoying thingy.")
time.sleep(2)
print ("Have fun!")
time.sleep(2)
password = input("Enter new password:")
time.sleep(2)
print ("Your password is %s, remmeber this." % password)
name = input ("What is your name?")
time.sleep (1)
I have this and want to use %s for 2 things. How do I do this?
Pass a tuple as the argument after the % operator.
print("Your name and password are: %s, %s" % (name, password))
This is called (old) printf-style String Formatting, and it can get pretty fancy.

Is it possible to reuse an 'if' statement?

So I am working on a project and I was wondering if it is possible to reuse an 'if' statement. For example, my code right now is
import re
import string
userinput=''
print "Hello! What is your name?"
userinput = str(raw_input("My name is "))
if not re.search(r'[A-Za-z]', userinput):
print "That isn't a name!"
print str(raw_input("My name is "))
and it prints
Hello! WHat is your name?
My name is 86124674983#
That isn't a name!
My name is 986421674941
986421674941
As you can see, it recognizes anything other than letters as an invalid entrance, but it only does it once. If you input symbols the second time it prompts you for a name, it takes that random input and prints it. I want it to print
Hello! WHat is your name?
My name is 86124674983#
That isn't a name!
My name is 986421674941
That isn't a name!
My name is Eli
Sorry if this confuses anyone. If you need anything clarified don't hesitate to ask. Thanks very much in advance!!
Use a while loop:
print "Hello! What is your name?"
while True:
userinput = raw_input("My name is ")
if not re.search(r'[A-Za-z]', userinput):
print "That isn't a name!"
else:
break
print userinput
Note you don't print a raw_input() - or make it str (it already is). All you need is the raw_input('text') and it will display text.
Use a while-loop:
print "Hello! What is your name?"
while True:
userinput = raw_input("My name is ")
if re.search(r'[A-Za-z]', userinput):
break
print "That isn't a name!"
print userinput

Alphabet check as input

Hi I just start learning python today and get to apply what I learning on a flash cards program, I want to ask the user for their name, and only accept alphabet without numbers or symbols, I've tried several ways but there is something I am missing in my attempts. Here is what I did so far.
yname = raw_input('Your Name ?: ')
if yname.isdigit():
print ('{0}, can\'t be your name!'.format(yname))
print "Please use alphbetic characters only!."
yname = raw_input("Enter your name:?")
print "Welcome %s !" %yname
but I figured in this one is if the user input any character more than one time it will eventually continue...So I did this instead.
yname = raw_input("EnterName").isalpha()
while yname == True:
if yname == yname.isalpha():
print "Welcome %s " %(yname)
else:
if yname == yname.isdigit():
print ("Name must be alphabetical only!")
yname = raw_input('Enter Name:').isalpha()
This while loop goes on forever, as well as I tried (-) and (+) the raw input variable as I've seen in some tutorials. So I thought of using while loop.
name = raw_input("your name"):
while True:
if name > 0 and name.isalpha():
print "Hi %s " %name
elif name < 0 and name.isdigit():
print "Name must be Alphabet characters only!"
try:
name != name.isalpha():
except (ValueError):
print "Something went wrong"
This will check for both alphabet in the raw_input and check for the length of the name as I see you tried to do in your last try.
import string
import re
name = re.compile(r'[a-zA-Z]+') #This will check for alphabet.
yname = raw_input("Your Name:") #ask the user for input.
while not name.match(yname):
print "invalid characters"
yname = raw_input("Your Name:")
if 5<=len(yname)<=10:
print "Hi,", yname, "!"
elif len(yname)>10:
print "too long!"
elif len(yname)<5:
print "too short!"
You can rearrange your last attempt a bit to get what you want:
while True:
name = raw_input("your name") # ask inside the loop
if name and name.isalpha():
print "Hi %s " %name
break # leave the loop if done
elif name and name.isdigit():
print "Name must be Alphabet characters only!"
else:
print "Please enter something"
Note that if name will be True if name != "".
name = raw_input('Enter your name: ')
while not name.isalpha():
print 'Invaid characters in name'
name = raw_input('Enter your name: ')
Use regexes:
import re
regex = re.compile("^[a-zA-Z]+$")
valid_name = False
while not valid_name:
user_name = raw_input("EnterName")
if not regex.search(user_name):
print "this can't be your name"
else:
print "Hi there, {0}".format(user_name)
valid_name = True
Also, please take note that programmers often make false assumptions about human names
Edit: as an alternative you can skip compiling a regex and just use the pattern in place:
if not re.search("^[a-zA-Z]+$", user_name):
...
However, since you're doing it in a loop compiled version would have slightly better performance, since re.search actually compiles a regex behind the scenes each time invoked.
Also, please note I've changed match to search and slightly modified a regex since there're some differences and it appears tome me that search suits your situation more.

Categories