Python Forum
How to print number from user
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print number from user
#1
I am newbie here. I don't have knowledge of python program. after some reading I tried to write one program I just want to write simple program that will take input name from user and print the name on screen.

I have this program. when I enter my name I get error

>>> person = input('Enter your name: ')
print('Hello', person)
Enter your name: rehan

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    person = input('Enter your name: ')
  File "<string>", line 1, in <module>
NameError: name 'rehan' is not defined
>>>  
Reply
#2
print('Hello ' + person)
or if you are running python 3.6:

print(f'Hello {person}')
Reply
#3

Thank you very much @Larz60+
Still this does not print name

>>>  person = input('Enter your name: ')
print('Hello', + person)
  File "<pyshell#0>", line 2
    person = input('Enter your name: ')
    ^
IndentationError: unexpected indent
>>>  person = input('Enter your name: ')
print(f'Hello {person}')
  File "<pyshell#1>", line 2
    person = input('Enter your name: ')
    ^
IndentationError: unexpected indent
Reply
#4
print('Hello', + person)
remove the comma

I strongly recommend you start using the f-string method (first appears in python 3.6),
this is going to start showing up in all new code, so get used to it now and you will be in compliance later.
It is so much better that the old formatting methods.

also you get an indentation error because you have an indentation error.
all code should start in column 1 here. see: https://www.python-course.eu/python3_blocks.php
Reply
#5
(Dec-01-2017, 05:23 PM)Larz60+ Wrote:
print('Hello', + person)
remove the comma

I strongly recommend you start using the f-string method (first appears in python 3.6),
Hi Larz60+
Your code is not working. I get same error but below code is working for me

>>> a = raw_input()
5
>>> print a
5
>>> 

This code promote user to enter number and the print the value of number.

>>> b = raw_input()
ram
>>> print b
ram
Reply
#6
You are using antique outdated python.
nevertheless:
print('print('Hello ' + person)
cretainly does work. Look at my post carefully:
espically where I state:
Quote:remove the comma
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 912 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  Print Player Name and Number of Pokemon webmanoffesto 1 1,115 Aug-23-2022, 08:54 PM
Last Post: deanhystad
  print two different sequence number mantonegro 2 1,679 Nov-16-2020, 06:19 PM
Last Post: mantonegro
  Print the number of items in a list on ubuntu terminal buttercup 2 1,957 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,760 Jun-18-2020, 04:59 PM
Last Post: QTPi
  if the input is not number, let user input again teatea 14 8,705 Jun-02-2020, 07:03 PM
Last Post: ibutun
  Print 'X' a number of times Than999 1 2,662 Jan-18-2020, 06:41 PM
Last Post: Larz60+
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 2,982 Jan-03-2020, 10:34 AM
Last Post: perfringo
  Find Average of User Input Defined number of Scores DustinKlent 1 4,328 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  print number of a list line per line lateublegende 2 2,734 Mar-20-2019, 04:07 PM
Last Post: lateublegende

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020