How to get user input? Python - python

I'm very new to this, so please be gentle!
As a little bit of work for my Python course, I am learning to run user input code. I put together the below code but when I run it using command-B, it asks me the 'What's your name?' question, but when I type in my name and click enter, nothing happens? For info, I am using Python 3.7, and using SublimeText.
I am 100% sure this is an easy answer, but surprisingly I cannot find the answer, and I have searched a little bit on here, and generally via Google, etc.
name=input("What's your name?:")
print("Hi",name,"how do you do.")
age=input("How old are you",name,"?:")
print("Great",name,"I'm",age,"years old too.")
city=input("Which city do you come from",name,"?:")
print("What a coincidence, I am from",city,"too.")
print(name,", here is your record //")
print(name, age, city)
Thanks for any help, and if you guys have an tips for a super newbie, it would be much appreciated!

You are having some issues because you have few syntax errors. The following is your code with the corrections. There you go:
name = input("What's your name?:")
print("Hi "+ name + " how do you do.")
age = input("How old are you " + name + " ?:")
print("Great " + name + "I'm " + age + " years old too.")
city = input("Which city do you come from " + name + " ?:")
print("What a coincidence, I am from " + city + " too.")
print(name + " , here is your record //: ")
print(name + " " + age + " " + city)
Good luck with learning Python :)

a = input() //for string
b = int(input()) //for int
c = float(input()) //for float

name=input("What's your name?:")
print("Hi"+name+"how do you do.")
age=input("How old are you "+name+" ?:")
print("Great "+name," I'm ",age+" years old too.")
city=input("Which city do you come from "+name+" ?:")
print("What a coincidence, I am from "+city+" too.")
print(name+", here is your record //")
print(name+age+city)
Use + for concatenation inside print

Related

PY Chatbot Repeating user input prompt

New to Python here. I've been coding a small chatbot for a while now, and when I prompt the user to enter something instead of answering with the correct response the bot just repeats the prompt. Everything before that works just fine, but it gets stuck in a loop of asking the user for an input.
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.");```
Your while loop does not have an exit condition. It will repeat forever since 1
is always equal to 1.
Instead, you could do while (str1 != "quit") which would stop the while loop once the user enters "quit" in the prompt.
Also, side note, you should not be using semicolons at the end of lines in Python.
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while (str1 != "quit"): # You need an exit condition here
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.")
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.")
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)")
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)")
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + len(User_Name) + "letters long. (I read this as you asking how long your name is.")
Fix indentation
Convert output from len into string
print("Hello " + User_Name + """.
Tonic is a simple Python chatbot made in order to test things such as boolean logic
and variable definition.
(Also remember to speak to the bot in lowercase, without punctuation.)""")
while 1 == 1:
str1 = input("Say something: ")
#"hello" //////////////////////////////////////////
if "hello" in str1:
print("Hi " + User_Name + "! (I read this as the 'hello' greeting.");
#"hi" //////////////////////////////////////////
if "hi" in str1:
print("Hi " + User_Name + "! (I read this as the 'hi' greeting.");
#"how are you?" //////////////////////////////////////////
if "how are you" in str1:
print("good, how about you? (I read this as you asking 'how are you'.)")
mood = input("Enter Mood: ")
if "good" in mood:
print("Nice to hear " + User_Name + "! (I read this as you being in a good mood.)");
if "bad" in mood:
print("I hope you feel better soon, " + User_Name + "! (I read this as you being in a bad mood.)");
#"name length" //////////////////////////////////////////
if "name length" in str1:
print( "Your name is " + str(len(User_Name)) + "letters long. (I read this as you asking how long your name is.");
i just found a module which you can create a chatbot with just a handful lines of code
refer to this https://pypi.org/project/prsaw/
or
from prsaw import RandomStuff
random = RandomStuffV2()
str1 = input("message: ")
response = random.get_ai_response(str1)
print(response)

How to add a space between 2 inputs in print?

So the code I'm doing is this:
first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
print("Hi there " + first_name + last_name)
What I want to do is make a space so when I run it it shows a space between first_name and last_name.
When I run the program, it shows:
What is your first name? px1se
What is your last name? unknown
Hi there px1seunknown
I wanna get a space between the "px1se" and the "unknown".
For your print statement, try the f-string:
print(f'Hi there {first_name} {last_name}')
Here is the documentation
Or, alternatively,
print("Hi there " + first_name + " " + last_name)
or
print("Hi there {} {}".format(first_name, last_name))
try :
print("Hi there " + first_name +" "+ last_name)
Plenty of sensible approaches offered already, but I thought it might be worth adding the old style formatting to the list.
print("Hi there %s %s"%(first_name,last_name))

Get Input from User then Use it in a Sentence

Hello everyone I need your help.
I am running this plain code on atom
name =input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are already " + age + " years old, " + name + "!")
it is showing me the sand time icon at the bottom and doesn't give any output.
for other commands like
name = "Nick"
print(name)
is working perfectly.
I dont know what to do please help.
ptyhon version is 3.6.9

TypeError: '>=' not supported between instances of 'int' and 'str' even with adding int to the front of score

score = int(0)
name = str(input("What is your name?"))
print("Hello " + name + "!")
pi = input( name + ", can you tell me the value of pi? ")
if score >= '6' :
print ("congratulations, " +name + " you passed the exam! You'll be richer than your wildest dreams!" )
else:
print: ("Listen, " +name + " you screwed up big time, with grades like this you'll be stuck working in a Best Buy for the rest of your life")
The problem is the if score statement, that's where I get the error. I need to have this "quiz" graded. I've been working on this all night, I'm not a good coder. Please help me
thank you in advance
You are comparing int (score) with string ('6'). Try:
if score > 6: # <--omit the quotes
etc...
The int in score = int(0) isn't neccessary. You can just write score = 0. For the comparison, do you really want to use the number 6 as a string? If not then just do this score >= 6 and it should work. I didn't test it but the code below should work.
score = 0
name = input("What is your name?")
print("Hello " + name + "!")
pi = input(name + ", can you tell me the value of pi?")
if score >= 6:
print("congratulations, " + name + " you passed the exam! You'll be richer than your wildest dreams!")
else:
print("Listen, " + name + " you screwed up big time, with grades like this you'll be stuck working in a Best Buy for the rest of your life")

Python: Loop Prompt Back Around

A lot of the errors of this Python file have been fixed but there is one last thing it's not working. I need the else statement to loop back to ask the question again if neither Yes or No is entered. You can probably see by the code what I was going for but I'm probably not even on the right track. Can someone help me with this one last thing?
#These are my variables which are just strings entered by the user.
friend = raw_input("Who is your friend? ")
my_name = raw_input("Enter Your Name: ")
game_system = raw_input("What's your favorite game system? ")
game_name = raw_input("What's your favorite game for that system? ")
game_status = raw_input("Do you have the game? (Yes or No) ")
game_store = raw_input("What is your favorite game store? ")
game_price = raw_input("What's the price of the game today? Enter a whole number. ")
#This is what is printed after all the prompts. There is no condition for this print.
print "I went outside today and my friend " + friend + " was outside waiting for me. He said \"" + my_name + ", did you get the new " + game_system + " game yet? You were getting " + game_name + " today, right?\""
#If the condition on the Yes or No question is yes, then this code runs.
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
#If the condition is No, then this code runs.
elif game_status == "No":
print "\"Well let's go get it today and we can come back here and play it!\" We went down to " + game_store + " and baught " + game_name + " for $" + str(game_price) + " and we went back to my house to take turns playing until " + friend + " went home. Today was a good day. (Now try again with the No option!)"
#If the condition meets neither Yes or No, then this code runs, sending the user back to the same question again. This repeats until a condition is met.
else:
raw_input("That answer didn't work. Try again? Do you have the game? (Yes or No) ")
This:
if game_status: "YES"
isn't how you make an if statement. You're treating it like the syntax is
if some_variable: some_value
and if the variable has that value, the if statement triggers. In fact, the syntax is
if some_expression:
and if the expression evaluates to something considered true, the if statement triggers. When you want the if statement to trigger on game_status equalling "YES", the expression should be game_status == "YES", so the if line should go
if game_status == "YES":
Similarly, the elif line should go
elif game_status == "NO":
I would encourage to you always break to a new line after a conditional...
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
anything that is indented after the "if game_status:" will get run. And it reads better.
edit:: if you use single quotes for all strings then you don't need to escape the double quotes...
print '"You know I got it, man!" I said. "Awesome! Let\'s play it," he said. "I heard ' + game_name + ' is supposed to be amazing!" We went back inside and took turns playing until ' + friend + ' had to go home. Today was a fun day.'
it's a matter of preference...but may look less cluttered.

Categories