I have seem to forgotten how to use python.. print is not printing the name? I think am confusing C syntax with python
name = raw_input("Enter your name")
print "welcome, %s" name
You missed %. You need to be doing:
print "hello %s" %name
Using format() is another way to go about it:
print "welcome, {}".format(name)
Looks like you just forgot a % in there.
print "welcome, %s" % name
When you have a string you want to print with a variable, you have four options:
Option 1: Use percent
name = raw_input("Enter your name")
print "Welcome, %s" % name
Option 2: Use {}
name = raw_input("Enter your name")
print "Welcome, {}".format(name)
Option 3: Use a comma
name = raw_input("Enter your name")
print "Welcome,", name
Option 4: Use a plus
name = raw_input("Enter your name")
print "Welcome, "+name
Use a comma if the variable is at the end of the sentence, and use plus if it is in the middle.
Related
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.
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)
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
I'm fairly new to Python, and I'm working on a few things that would involve names. Something such as:
example = raw_input ("What is your name?: ")
How would I go about using this to have them answer it, and reprint their name such as:
print "Hello, {name}!"
You were very close, you already had the correct template, now you have to tell it what the {name} should be replaced by:
>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'
See the docs for more information on using str.format.
print "Hello, " + example + "!"
There are several ways to acomplish your goal.
As was mentioned, you could use:
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"
or,
>>> example = "Foo"
>>> "Hello, {name}!".format(name=example)
'Hello, Foo!'
You could also use,
name = raw_input("What is your name?: ")
print "Hello, %s!" % name
print raw_input('Enter name :')
For Python 3.5 users, it can work like this:
name = input("What is your name? ")
print "Hello, %s!" % (name)
That should work.
name = raw_input ("What is your name?: ")
print "Hello, " + name + "!"
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.