How to make an abbreviation for line of code? - python

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)

Related

Why can't I return my "cmds" variable in my code?

I want to create a command robot but I cant return cmds(variable). It always crashes not giving me anything. The output is just blank after I enter n in variable con. I've been working on the problem for the past 2 days. It's really annoying and I hope someone can help.
import time
print("Hi User! Pick a name for me!")
time.sleep(1)
name = input("Name for Bot: ")
g = print("Ooh", name + "!", "thats a cool name!")
print("What should I call you?")
time.sleep(1)
user_name = input("Your name: ")
g1 = print(user_name + "...", "cool name!")
time.sleep(1)
cmds = input("Say any command you would like :) - ")
def slap(cmds):
slap_p = input("Who do you want to slap: ")
caption_for_slap = input("Caption for slapping: ")
print("Loading...")
time.sleep(3)
print(caption_for_slap, "👊👊👊👊👊👊", ". You deserved it", slap_p)
con = input("Would you like to continue y/n - ")
def con_p(cmds, con):
if con == "y":
return slap()
elif con == "n":
return cmds
con_p()
if cmds == "./slap":
slap(cmds)
while True:
if cmds == "br":
break
def about_rb():
if cmds == "./about_rb":
print("I am a robot named", name + ".", "Thanks to", user_name, "for picking that name for me!")
time.sleep(1)
print("My age is 35 :)")
if cmds == "./about_rb":
about_rb()
def question():
if cmds == "do you like Siri or Cortana":
print("I love all robots! I dont really have a preference for both ♥")
Part 1: arguments for function calls
When issuing ./slap as a command, there are two problems in this part:
con = input("Would you like to continue y/n - ")
def con_p(cmds, con):
if con == "y":
return slap()
elif con == "n":
return cmds
con_p()
In the final line of this block, the newly created function con_p is called, but without any of the arguments (cmds, con). Since there is no default value for them either, the script crashes.
To resolve this, we just need to provide the required arguments; change the last line to:
con_p(cmds, con)
You might think that this is a bit unnecessary, as you've already used those names in the line def con_p(cmds, con):, but that's just what the provided arguments will be known as inside the function con_p(), not where they're pulled from.
The second problem occurs when con_p() is called with con = "y": then con_p() calls slap() again without arguments. We need to replace return slap() with return slap(cmds).
Side note: I don't know if there is any further intent with the code block I copy/pasted above, but as is, the con_p() function can be removed. The entire block can be replaced with:
con = input("Would you like to continue y/n - ")
if con == "y":
return slap(cmds)
cmds also isn't used inside slap(), so we can also just define it as def slap(): instead of def slap(cmds):.
Part 2: infinite loop
As the code is executed from the first line to the last, the following block of code will always keep looping unless cmds equals br.
while True:
if cmds == "br":
break
Since we cannot adjust cmds during the loop, and it is asked only once, we have three scenarios based on the value of cmds:
./slap so we can slap people, and after we quit slapping (con == "n") we will get in the infinite loop
br so we don't get caught in the loop, no other commands are executed, and the program exits without doing anything
any other value for cmds which doesn't get evaluated since we just end up in the loop forever.
I don't know for sure the intended purpose of this loop, but I think you want to put a loop around the entire code that asks for a command input and executes them, as such:
def slap(cmds):
[...]
def about_rb():
[...]
while True:
cmds = input("Say any command you would like :) - ")
if cmds == "./slap":
slap(cmds)
if cmds == "./about_rb":
about_rb()
if cmds == "br":
break
Here, we first define all our functions. Then in an endless loop we keep asking the user for a new command, and run that command. Only when the command is "br", we break the loop and the computer continues with the code after the loop (if there is any).
Putting it all together
# imports
import time
# define functions
def slap(cmds):
slap_p = input("Who do you want to slap: ")
caption_for_slap = input("Caption for slapping: ")
print("Loading...")
time.sleep(3)
print(caption_for_slap, "👊👊👊👊👊👊", ". You deserved it", slap_p)
con = input("Would you like to continue y/n - ")
if con == "y":
return slap(cmds)
def about_rb():
if cmds == "./about_rb":
print("I am a robot named", name + ".", "Thanks to", user_name, "for picking that name for me!")
time.sleep(1)
print("My age is 35 :)")
# name the bot
print("Hi User! Pick a name for me!")
time.sleep(1)
name = input("Name for Bot: ")
g = print("Ooh", name + "!", "thats a cool name!")
# name the user
print("What should I call you?")
time.sleep(1)
user_name = input("Your name: ")
g1 = print(user_name + "...", "cool name!")
time.sleep(1)
# start main loop
while True:
# get command
cmds = input("Say any command you would like :) - ")
if cmds == "br":
break
if cmds == "./slap":
slap(cmds)
if cmds == "./about_rb":
about_rb()
if cmds == "do you like Siri or Cortana":
print("I love all robots! I dont really have a preference for both ♥")

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

Usage of raw_input in 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)

Passing a value to end message of game

I have this hang man code but i want to add parameter passing to it
import time
import random
#Procedure
def Username():
name = raw_input("What is your name?")
print "Hello, "+ name, "Time to play hangman!"
print "\n"
def loading():
#Makes the user wait for 1 second
time.sleep(1)
print "Start guessing..."
time.sleep(0.5)
return
def randwords():
global words
words = ["keyboard", "motherboard", "python", "powersupply"]
words = random.choice(words)
#Main Program
Username()
loading()
randwords()
guesses =""
turns = 12
#while loop
while turns > 0:
failed = 0
for char in words:
if char in guesses:
print char,
else:
print"_",
failed += 1
if failed == 0:
print "\nyou won Well Done"
break
print
guess = raw_input("guess a character:")
guesses += guess
if guess not in words:
turns -= 1
print "wrong\n"
print "you have", + turns, "more guesses"
if turns == 0:
print "you lose GAME OVER\n"
input()
Username()
waiting()
Random
I want it to take the name value, user types in at the start and then pass that value into the end messages.
Example
print "you have won (NAME) Well Done"
or
print "you lose (NAME) GAME OVER"
Please help me somebody because I can solve the problem every time I try I get a error message.
You should declare 'name' as global variable.
name = '' # before def Username
And before you assign a value to this variable, do following:
global name
name = raw_input("What is your name?")
You may then use this variable anywhere in the script to print its value.
print name, "you lose GAME OVER\n"
P.S : To write to a gllobal variable, you must precede with:
global <variable-name>
whereas, you may read its value in normal way
You can simply return the name from the function 'Username()'.
def Username():
...
return name
def main():
...
name = Username()
...

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

Categories