My apologies as I am very new to Python and coding in general but I am trying an exercise on creating functions and formatting all in the same. Here I have a function I wrote for practice. But I can't get it to run correctly. What am doing wrong with creating this function.
My code:
def greeting(name):
name = input("What's your name: ")
return "Hi %s" % name
print(greeting())
The error:
TypeError: greeting() missing 1 required positional argument: 'name'
Thank you for your help. :)
You don't need to give name as a parameter to the greeting function.
Try this:
def greeting():
name = input("What's your name: ")
return "Hi %s" % name
print(greeting())
You don't want to take name as an argument, because you are creating that variable within the function in your call to input()
def greeting():
name = input("What's your name: ")
return "Hi %s" % name
You also do not need to print() the function, because it's return value (a string) is printed by default. If you saved the return value of greeting() into an object, you could then print that object
the_greeting = greeting()
print(the_greeting)
Related
I keep getting this error no matter what I do. Its suppose to make a new data but it does not seem to want to work. The full error is:
line 35, in make_list
pet = petspgm.PetData(pet_name, pet_type, pet_age)
builtins.AttributeError: module 'petspgm' has no attribute 'PetData'
Here's the code for petspgm
class PetData:
# The __init__ method initializes the attributes
def __init__(self, pet_name, pet_type, pet_age):
self.__pet_name = pet_name
self.__pet_type = pet_type
self.__pet_age = pet_age
# This method accepts an argument for the pet's name
def set_pet_name(self, pet_name):
self.__pet_name = pet_name
# This method accepts an argument for the pet's type
def set_pet_type(self, pet_type):
self.__pet_type - pet_type
# This method accepts an argument for pet's age
def set_pet_age(self, pet_age):
self.__pet_age = pet_age
# This method returns the pet's name
def get_pet_name(self):
return self.__pet_name
# This method returns the pet's type
def get_pet_type(self):
return self.__pet_type
# This method returns the pet's age
def get_pet_age(self):
return self.__pet_age
and the code for the other one
import petspgm
def main():
# Get a list of pet objects
pets = make_list()
# Display the data in the list
print ('Here is the data you entered: ')
display_list(pets)
# This function gets data deom rhe user for 3 pets. It returns a list of pet objects containing the data
def make_list():
# Create an empty list
pet_list = []
# Add 3 pet objects to the list
print ('Enter the data for 3 pets')
for count in range(1, 4):
# Get the pet data
print ('Pet number ' + str(count) + ':')
pet_name = input("Enter your pet's name: ")
pet_type = input('Enter the type of pet: ')
pet_age = input("Enter your pet's age: ")
print
# Create a new Pet Data object in memory and assign it to the pet variable
pet = petspgm.PetData(pet_name, pet_type, pet_age)
# Add the object to the list
pet_list.append(pet)
# Return the list
return pet_list
# This function accepts a list containing Pet Data objects as an argument and displays the data stored in each object
def display_list(pet_lists):
for item in pet_list:
print ("Pet's name is: " + item.get_pet_name())
print ("Pet's type is: " + item.get_pet_type())
print ("Pet's age is: " + item.get_pet_age())
# Call the main function
main()
input('Press Enter to continue')
I have tried adding a pet data code but then the entire code crashes and the file wont be able to be opened by python. I have tried using different wording in the coding and it give the same error. I've also just tried to make the list myself but it just crashes python.
I'm following the Microsoft introduction to Python course for beginners on edX, I'm having trouble in their second module where they ask you to create function that adds the "Doctor" title to a user inputted name.
This is the advice they provide:
Define function make_doctor() that takes a parameter name
get user input for variable full_name
call the function using full_name as argument
print the return value
create and call make_doctor() with full_name argument from user input - then print the return value
This is what I have so far:
def make_doctor(name):
full_name = print("Doctor" + input().title())
return full_name
print(name)
Would appreciate any help.
Python is an off-side rule language:
Python Reference Manual (link)
Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
turn is used to determine the grouping of statements.
In contrast to others such as curly-bracket languages, indentation is (generally) not stylistic, but required in order to group statements. Therefore, your code should look like this:
def make_doctor(name):
return "Doctor" + name
full_name = input()
print(make_doctor(full_name))
def make_doctor(name):
# add the Doctor title to the name parameter
d_name = 'Doctor '+name
# print the return value
print(d_name)
return d_name
# get the user input for the variable full_name
full_name=input('Enter your full name: ')
# pass full_name as an argument to make_doctor function
doc = make_doctor(full_name)
# print return value
print(doc)
def make_doctor(name):
full_name = input()
return full_name
print('Doctor ' + full_name)
Can I get class in the same way as I can get %s? Is this possible? I want to access class with the help of the string. Examples below show what I have in mind.
string example
name = raw_input("who are you?")
print "hello %s" % (name,)
class example
class Temp:
def __init__(self, name):
self.n = name
testclass = Temp("this is placeholder class")
classname = "testclass"
print %c.n % (classname)
Expeted output:
"this is placeholder class"
like in:
print testclass.n
EDIT: More detailed infromation of what I want to do:
In function instead of writing every possible combination of variables I'd like to do one code that adjusts to query. Example with string
name1 = "John"
name2 = "Doe"
inpt = raw_input()
inpt
name = "name%s" % str(inpt)
print "Hi %s" % (name)
Outcome hear will be name1 or name2 depending on which number I type in raw_input but instead of that I'd like to have outcome John or Doe depending on which number I type in but not with the string but class. And I don't have to input but I want filename in string to become variable name that will give me output depending on what is contained in asked class subclass (in example above .n). I hope I didn't make this even more confusing.
I would like to know where am lagging, Looking for your advices..
class Student_Record(object):
def __init__(self,s):
self.s="class_Library"
print"Welcome!! take the benifit of the library"
def Student_details(self):
print " Please enter your details below"
a=raw_input("Enter your name :\n")
print ("your name is :" +a)
b=raw_input("Enter your USN :\n")
print ("Your USN is:" ,int(b))
c=raw_input("Enter your branch :\n")
print ("your entered baranch is" +c)
d=raw_input("Enter your current semester :\n")
print ("your in the semester",int(d))
rec=Student_Record()
rec.Student_details(self)
I am getting this error ..
TypeError: init() takes exactly 2 arguments (1 given)
Your Student_Record.__init__() method takes two arguments, self and s. self is provided for you by Python, but you failed to provide s.
You are ignoring s altogether, drop it from the function signature:
class Student_Record(object):
def __init__(self):
self.s = "class_Library"
print"Welcome!! take the benifit of the library"
Next, you are calling the method rec.Student_details() passing in an argument, but that method only takes self, which is already provided for you by Python. You don't need to pass it in manually, and in your case the name is not even defined in that scope.
if you do
class Student_Record(object):
def __init__(self, s):
self.s = ""
def Student_details(self):
print " Please enter your details below"
when you create the object of class Student_Record it should accept a parameter despite for itself (self). so it looks like:
record = Student_Record("text")
and in __init__ you can do whatever with the passed-in variable s. For example, self.s = s and you can call it anywhere in the class with self.s because it has been initialized.
Your code should be like this..(python indent):
class Student_Record(object):
def __init__(self,s="class_Library"):
self.s=s
print"Welcome!! take the benifit of the library"
def Student_details(self):
print " Please enter your details below"
a=raw_input("Enter your name :\n")
print ("your name is :" +a)
b=raw_input("Enter your USN :\n")
print ("Your USN is:" ,int(b))
c=raw_input("Enter your branch :\n")
print ("your entered baranch is" +c)
d=raw_input("Enter your current semester :\n")
print ("your in the semester",int(d))
rec=Student_Record()
rec.Student_details()
s in def __init__ should have a default value or you can pass a value from rec=Student_Record().
I am wondering, is there a way to use the input() function inside a user-defined function? I tried doing this
def nameEdit(name):
name = input()
name = name.capitalize
return name
Using input is fine. However, you aren't calling name.capitalize; you're just getting a reference to the method and assigning that to name. [Further, as pointed out by Bob, your function doesn't need a name argument.] The correct code would be
def nameEdit():
name = input()
name = name.capitalize()
return name
or more simply:
def nameEdit():
return input().capitalize()
Are you talking about asking for input from a user from a method? If so then this would be what you're looking for:
def nameEdit():
name = input("Enter your name: ")
name = name.capitalize()
return name