Really easy stuff, sorry in advance for the idiotic question but I'm just not understanding how to do this... I want to make a function that asks the user's name and then greets the user by name.
so if I were to do greet(yo)...
What's your name? Tom
Yo, Tom
I've got this so far:
def greet(yo):
print("Whats your name")
raw_input(name)
return yo + name
Save the return value from raw_input into a variable:
def greet(yo):
name = raw_input("What's your name: ")
return yo + name
print greet("hi! ")
Demo:
>>> greet('hi! ')
What's your name: Tom
'hi! Tom'
For python 2, use raw_input to get input.
For python 3, use input to get input.
def greet(greeting):
name = raw_input("Hi, whats your name?") #python 2
return greeting + name
print greet("Hi, ")
# Python program that asks the user to enter their name, and then greet them.
name = input("Hello, What's your name?")
# Then type in your name.
print("Hello " + name+ " it's nice to meet you"+ "!")
For example:
Hello, What's your name?Bob
Hello Bob it's nice to met you!
Related
name = input(enter name)
age = input(age)
print(“My name is print(name). I’m print(age) years old.”)
Nobbie experiment.
Beginner level task.
And the above query came to my mind.
name = input("enter name: ")
age = input("age: ")
print(f"My name is {name}. I am {age} years old")
Study and try to understand Keshav V. answer using f-strings, this is the "modern" approach and will serve you well time after after time.
A more long handed approach would be to rewrite your program like this (as a stepping stone to understanding the f-string format)..
name = input("enter name ")
age = input("age ")
print("My name is", name, "I’m", age, "years old.")
Notice that print will accept as many items as you want to print and place them on the same line.
You could also use f-string formatting once you have capture the input in name and age variable.
print(f"My name is {name}. I'm {age} years old")
My name is laura. I'm 18 years old
You can simply use python f-strings.
name = input('Enter your name ')
age = input ('Enter your age ')
print(f'My name is {name}. I\'m {age} years old.')
If you also want you can go with the format function.
or this
name = input('Enter your name ')
age = input ('Enter your age ')
print('some text' + name + 'also some text')
good luck at programming,
You can also try string formatting.
name = input('Enter your name ')
age = input ('Enter your age ')
print('My name is {}. I\'m {} years old.').format(name,age)
More About String Formatting
Would be a good idea to take some time to read a Python book.
name = input("enter name")
age = input("age")
print( f"My name is {name}. I'm {age} years old.")
You can use a formatted string for this:
def name_and_age(name, age):
return f"My name is {name} and I'am {age} years old"
print(name_and_age('Max', 35))
#output: My name is Max and I'am 35 years old
I don't know how to make the script keep asking for the pet's name if I enter a name that is not in the myPets list.
So if I type in Tiny ( a name not in the list) the script runs and closes after it prints out ('I do not have a pet named '+ name) 'I do not have a pet named Tiny.
I want to put a for loop in the script so it will ask me again for an input.
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter Pets Name:')
name = input()
if name not in myPets:
print('I do not have a pet named '+ name)
else:
print(name + ' is my pet.')
Not sure about using a for loop, but you can definitely use an infinite while loop to make this happen:
myPets = ['Zophie', 'Pooka', 'Fat-tail']
while True:
print('Enter Pets Name:')
name = input()
if name not in myPets:
print('I do not have a pet named '+ name)
else:
print(name + ' is my pet.')
break
You might want to use while loop here so that you can continue to ask for the correct input
Try with the while loop.
myPets = ["Zophie", "Pooka", "Fat-tail"]
def test():
while True:
name = input("Enter the pet name: ")
if name in myPets:
print(name + " is my pet.")
test()
else:
print("I do not have a pet named " + name)
break
test()
Hello Pete,
Thank you for your question. Please have a look at the code below where I try to answer it as much as possible. There are comments in every line explaining what we are doing along with the respective results. I certainly hope this helps, buddy. Keep coding in Python, this is a great and fun language to learn. I am also learning it right at this moment.
myPets = ['Zophie', 'Pooka', 'Fat-tail']
#Let us start by getting the Pet's name on this sentence
myPetName= input("What is your Pet's Name?\n")
#As long as the user keeps typing the wrong name, the
# routine will keep asking for your pet's name,
# for the example we will use the name "Zophie"
while myPetName != 'Zophie':
print(f"I do not have a pet named {myPetName}.")
#Now, let's ask one more time your pet's name.
myPetName= input("What is your Pet's Name?\n")
# Now that the user types in the right name of your pet
# in this case we are using Zophie, let's print it on the screen
print(f"{myPetName} is certainly the name of my pet.")
It's a little elementary, but you can just replace your if with a do-while construct like so:
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter Pets Name:')
name = input()
while name not in myPets:
print('I do not have a pet named '+ name)
name = input ()
print(name + ' is my pet.')
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 + "!"
So I just started trying out python yesterday and I want to code something where I can input text then the something will be added once enter is pressed
for example:
If I input: Sam Smith
After pressing enter . . .
I would get: Welcome Sam Smith
name = input('What is your name? ')
print('Welcome ' + name)
First, the user is asked for the name which is then stored in a variable. Then, a message is printed using the stored name.
Python2.x:
>>> name = raw_input("Please enter the name..")
Please enter the name..Sam Smith
>>> name
'Sam Smith'
>>> print "Welcome " + name
Welcome Sam Smith
Python3.x:
>>> name = input("Please enter the name..")
Please enter the name..Sam Smith
>>> name
'Sam Smith'
>>> print("Welcome " + name)
Welcome Sam Smith