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.
Related
I am having trouble passing a variable from one function to another:
def name():
first_name=input("What is your name? ")
if len(first_name)==0:
print("\nYou did not enter a name!")
return name()
else:
print("\nHello %s." % first_name)
return surname()
def surname():
last_name=input("\nCan you tell my your last name please?")
if len(last_name)==0:
print("\nYou did not enter a last name!")
return surname()
else:
print("Nice to meet you %s %s." % (first_name,last_name))
I want the last command to print the inputed first_name from def name() and last name from def surname()
I always get the error that first_name is not defined and I do not know how to import it from the first function. The error I get is:
print("Nice to meet you %s %s." % (first_name,last_name))
NameError: name 'first_name' is not defined
What am I doing wrong?
You need to pass the information in the function call:
def name():
first_name = input("What is your name? ")
if len(first_name) == 0:
print("\nYou did not enter a name!")
return name()
else:
print("\nHello %s." % first_name)
surname(first_name) # pass first_name on to the surname function
def surname(first_name): #first_name arrives here ready to be used in this function
last_name = input("\nCan you tell my your last name please?")
if len(last_name) == 0:
print("\nYou did not enter a last name!")
surname(first_name)
else:
print("Nice to meet you %s %s." % (first_name,last_name))
name()
def functionname(untypedparametername):
# do smth with untypedparametername which holds "Jim" for this example
name = "Jim"
functionname(name) # this will provide "Jim" to the function
You can see how they are used if you look at the examples in the documentation, f.e. here: https://docs.python.org/3/library/functions.html
Maybe you should read up on some of the tutorials for basics, you can find lots of them on the python main page: https://wiki.python.org/moin/BeginnersGuide
You can also use while loop to ask the names constantly until there is a valid input.
def name_find():
while True:
first_name=raw_input("What is your name? ")
if len(first_name)==0:
print("\nYou did not enter a name!")
return name_find()
else:
print("\nHello %s." % first_name)
return surname(first_name)
def surname(first_name):
while True:
last_name=raw_input("\nCan you tell me your last name please?")
if len(last_name)==0:
print("\nYou did not enter a last name!")
else:
print "Nice to meet you %s %s." % (first_name, last_name)
break
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)
In python, I am trying to make this code accept the user to move forward if he writes "True", and not if he writes "False" for the statement in User_Answer. When I run the code however, I get the "the answer is correct!"-part no matter what I write. The part I am having trouble with starts with "Test_Answer".
Could anyone help me with this?
name_list = ["Dean", "Bill", "John"]
enter_club = ["Enter", "enter"]
print ("THE CLUB - by Mads")
print (" ")
print ("""You approach a secret club called \"The club\". The club members are dangerous.
Make sure you tell the guard one of the members names.""")
print ("")
print ("Good evening. Before you are allowed to enter, we need to check if your name is on our list.")
def enter_the_club():
enter_now = input(" \nPress \"enter\" to enter the club... ")
if (enter_now in enter_club) == True:
print (" ")
print ("But as you enter, you are met with an intelegence test. \n It reads:")
check_name = input("What is your name? ")
def list_check():
if (check_name in name_list) == True:
print("Let me check.. Yes, here you are. Enjoy yourself, %s!" % check_name)
enter_the_club()
elif check_name.isalpha() == False:
print("Haha, nice try %s! Let's hear your real name." % check_name)
list_check()
elif (check_name in name_list) == None:
print ("You will need to give us your name if you want to come in.")
list_check()
else:
print ("I am sorry, but I can not find your name on the list, %s." % check_name)
print ("Are you sure that's your listed name?")
list_check()
list_check()
print ("But as you enter, you are met with an intelegence test.")
print (" ")
print ("It reads:")
Test_Answer = True
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
IQtest()
True is a boolean constant. What the user enters will be either "True" or "False", both character strings. Also, your elif condition cannot be true. What are you trying to do with three decision branches?
Without changing your code too much ... try this?
Test_Answer = "True"
def IQtest():
User_Answer = input("Is 18/4 % 3 < 18 True or False? ")
if Test_Answer == User_Answer:
print ("Great, %s, the answer is correct!" % check_name)
else:
print ("You are not allowed to enter before the answer is correct, %s!" % check_name)
IQtest()
Note that I've also corrected your "else" syntax.
Also, you have no value for check_name; I assume this is a global variable that you've handled elsewhere.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
i have no clue why there's an indentation area but it's realy stresssing me out
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)
if raw_input() = "yes"
print "what is your password?"
if raw_input() = "no"
print "Suit yourself, %s!" % (name)
first of all, your indentation is off when you write your if statements. You need to bring them back by one tab space.
Here is an example of the working code:
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
print "Now %s, are you ready for your password to be tested?" % (name)
# I fixed the indentation below:
if raw_input() == "yes": #<-- fixed comparison operator here and added colon
print "what is your password?"
if raw_input() == "no": #<-- fixed comparison operator here and added colon
print "Suit yourself, %s!" % (name)
Here I also changed the = sign to ==. The = sign is what you use when you are declaring a variable, like name = raw_input(), which creates the variable name with the value of the raw_input().
The == sign is what you use when you are comparing two things, like raw_input() == "yes", which checks whether or not the raw_input() value is equal to "yes".
A lot of issues with your program, so I would strongly suggest you to go through some tutorials
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
print "what is your password?"
if yes_no.lower() == "no":
print "Suit yourself, %s!" % (name)
Always assign your raw_inputs to a variable.
Ex:
Change this print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
to this:
name = raw_input("first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
Secondly
When ever you are using if, for, while etc you have to end the statements with a : and any statements which you want to execute as long as your condition is held true should be indented.
Ex:
if yes_no == 'yes':
print 'Ok'
print 'Ending program'
This way the layout of your program will be a LOT clearer to the user. Look over some tutorials and try them out
Thanks to Josh B. and letsc, I have now completed the code:
print "hello!"
print "I\'m Charlie and I\'m going to test the security of your password!"
print "first I\'d like to know a bit about you! What is your name? (No data is stored!!)"
name = raw_input()
print "Nice to meet you, %s!" % (name)
print "What is your date of birth?"
dob = raw_input()
print "OK! Those answers are used to help me but are forgotten when you close the window!"
yes_no = raw_input("Now %s, are you ready for your password to be tested?" % (name))
if yes_no.lower() == 'yes':
print "what is your password?"
passw = raw_input()
if passw == "password":
print "really? You\'ve got to be kidding me!"
if passw == name:
print "Anybody who knows your name will be able to guess that!"
if passw == dob:
print "Anyone who knows your birthday can guess that!"
if len(passw) < 5:
print "That password may not be long enough, %s" % (name)
if len(passw) > 20:
print "Are you sure you can remeber that?"
else:
print "That\'s a superb password, %s!!" % (name)
if yes_no.lower() == "no":
print "Suit yourself, %s!" % (name)
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.