This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 4 years ago.
I was trying to get some input in python and I have no idea what is my problem
name = input("What's your name?")
age = int(input("How old are you?"))
year = str((100 - age) + 2018)
print("Hello "+ name + ",in " + year + "you'll be 100 y.o")
and when I use my name as input like "shayan", thats came out:
name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'shayan' is not defined
I tri my code in "atom" , "sublime" , "visual studio code"
It's because you're on python 2, so input in python is basically eval(input(...)) in python 3, so it will take inputs as code, not strings, so gotta use raw_input in python 2:
name = raw_input("What's your name?")
age = input("How old are you?")
year = str((100 - age) + 2018)
print("Hello "+ name + ",in " + year + "you'll be 100 y.o")
Related
This question already has answers here:
Cannot change global variables in a function through an exec() statement?
(3 answers)
Closed 2 years ago.
I'm learning python and trying to use a function to choose and change a global variable.
I want to use the console to choose which variable to change and then choose the new value.
I'm using global inside the function to access the variables and I'm using exec() to proceed with the desired modification captured through input(). But something is not working.
Can someone please figure out what is wrong here?
name = "John"
age = 45
gender = "male"
def identify_yourself():
print("My name is " + name + ".")
print("I'm " + str(age) + " years old.")
print("I'm " + gender + ".")
def change_something():
global name, age, gender
something = input("Which variable do you want to change?\n> ")
# I then input "name"
new_value = input("Change to what?\n> ")
# I Then input "Paul"
exec(something + " = '" + new_value + "'")
identify_yourself()
identify_yourself()
# This first prints...
#
# My name is John.
# I'm 45 years old.
# I'm male.
change_something()
# On the second time this SHOULD print...
#
# My name is Paul.
# I'm 45 years old.
# I'm male.
#
# ... but it's not working.
# It keeps printing "My name is John" even after I run the change_something() function
The exec() uses globals() and locals() as defaults for their global and local variables. It defaults to changes in the locals(), not the globals. You therefore have to explicitly tell it to overwrite globals. You can do this the following way:
exec(something + " = '" + new_value + "'", globals())
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 3 years ago.
I have some code. When User write "Date" in input line, output must be date, like 2019-06-8 16:34:40
but program can not find input text - name 'date' is not defined
import datetime
x = input("Type your question here ... ")
########## Date ##########
now = datetime.datetime.now()
date = "Date"
if x == date:
print "Current date and time:"
print str(now)
########## (Can't find anything) ##########
else:
print("Something goes wrong ;( ")
Error -
NameError: name 'date' is not defined
In Python 3
import datetime
x = input("Type your question here ... ")
########## Date ##########
now = datetime.datetime.now()
date = "Date"
if x == date:
print ("Current date and time:")
print (str(now))
########## (Can't find anything) ##########
else:
print("Something goes wrong ;( ")
and the output is:
Type your question here ... Date
Current date and time:
2019-07-08 12:53:41.534617
https://pastebin.com/ztix8Aue
def namex3():
ask = input("What is your name?")
name = ask
for i in range(name):
print(name * 3)
namex3()
I'm trying tob define a function called “namex3” that takes an argument called
“name” and prints “hello " {name} three times on the screen, however I am getting an error that it Cannot be interpreted as an integer, how would I fix this?
Continuing from the comments above, fixed:
def namex3():
ask = input("What is your name?")
for i in range(3):
print(ask)
namex3()
OUTPUT:
What is your name?TFX
TFX
TFX
TFX
Try this if you intend to use a function :
def namex3():
print(*["hello " + input("What is your name?")]*3, sep = "\n")
namex3()
You almost had it:
def namex3()
name = input("What is your name? ")
for i in range(3):
print("Hello", name)
namex3()
Notice that every indent level is 4 spaces which is the adviced style in Python.
You could even shorten it to
name = "some name"
print(name * 3)
Or (with newlines):
print("{}\n".format(name) * 3)
As OP asked for a function that will print "Hello {name}" three times, here it is:
def name_three_times():
name = input("What is your name?")
print(f'Hello {name}\n' * 3)
Note the use of f-strings: https://realpython.com/python-f-strings/
That's because you're attempting range('hello')
range() expects integer arguments, but you are passing 'hello', which is a string.
I am trying to open up this on the terminal on my mac, but I keep getting name 'author' not defined when it clearly is.
def bibformat_mla(author, title, city, publisher, year):
author = input("enter author: ")
title = input("enter title: ")
city = input("enter city: ")
publisher = input("enter publisher: ")
year = input("enter year: ")
answer = author + ' , ' + title + ' , ' + city + ': ' + publisher + ', ' + year
return answer
bibformat_mla(author, title, city, publisher, year)
'author, title, city: publisher, year'
bibformat_mla("Jake, Matt", "Open Data ", "Winnipeg", "AU Press", 2013)
'Morin, Pat. Open Data Structures. Winnipeg: AU Press, 2013'
When you run the following:
bibformat_mla(author,title,city,publisher,year)
You are saying to the program that you have a variable called "author" which is ready to pass into biblformat(). This causes an error because the the variable isn't defined before the function is called.
I.e You're telling the function to expect a certain variable, and it throws an error at you because the variable doesn't actually exist yet.
From what it looks like you are trying to accomplish, you can simply call the function like this:
bibformat_mla()
You will also need to change your definition to this so that your function no longer expects the parameters:
def bibformat_mla():
With functions you can pass information as Parameters, in your function definition you are indicating that when you run the function you will also pass 5 variables with it.
From the looks of it you are setting the variables by user input and thus you do not need to pass parameters, deleting them should make the code work.
This:
def bibformat_mla(author, title, city, publisher, year):
Into this:
def bibformat_mla():
You need to decide whether your function will be accepting those strings as arguments or if it will prompt the user for them? There isn't much point requiring the user provide the values as arguments, then immediately overwriting them with values they enter.
So you have a choice.
perform the inputs prior to calling the function and pass the inputted values to the function.
remove the arguments to the function and allow the user to input the strings as part of bibformat_mla.
code:
def bibformat_mla1 (author,title,city,publisher,year):
return author + ' , ' + title + ' , ' + city + ': ' + publisher + ', ' + str(year)
def bibformat_mla2 ():
author = input ("enter author: ")
title = input ("enter title: ")
city = input ("enter city: ")
publisher = input ("enter publisher: ")
year = input ("enter year: ")
return author + ' , ' + title + ' , ' + city + ': ' + publisher + ', ' + year
print(bibformat_mla1("Jake, Matt", "Open Data ", "Winnipeg", "AU Press", 2013))
print(bibformat_mla2())
This question already has answers here:
How to compare multiple variables to the same value?
(7 answers)
Closed 3 years ago.
I want this program to be in a loop until the user puts DONE
import datetime
fileName =input('file name :')
fileName = fileName+'.csv'
access ='w'
currentdate=datetime.date.today()
with open (fileName,access)as newFile :
newFile.write('Name,Age,Nationality,Date\n')
while True:
name=input('Name:')
age=input('Age: ' )
nationality=input('Nationality: ')
newFile.write(name+','+age+','+nationality+',%s'%currentdate+'\n')
if name or age or nationality == 'DONE':
break
In the following IF statement in your code, nationality is the only variable being compared to the string value 'NONE'.
if name or age or nationality == 'DONE':
break
Try either of the following
if any(x == 'DONE' for x in [name, age, nationality]):
break
if 'DONE' in [name, age, nationality]:
break