function without printing none - python-3.x

for some reason I'm printing two Nones, can someone explain how to avoid that?
class Wizard():
def __init__(self,name, gender,power):
self.name = name
self.gender = gender
self.power = power
def fire(self):
print('My power is the fire')
def wind(self):
print ('My power is the wind')
def power_wizard(self):
if power == 1:
print(Wizard.fire(self))
else:
a=print(Wizard.wind(self))
# Create player
name = input('Enter your wizard name: ')
gender = input ('Enter the gender: ')
power = int(input ('Choose your power 1) Fire and 2) Wind:(only numbers)'))
player1 = Wizard(name,gender,power)
print(player1.name)
print(power_wizard(power))
print (The question is how can I not print None or why am I printing None)
My power is the fire
None
None
And also why are 2 None where are the coming from, because when de function wizard_power there only to results.

why am I printing None
When you execute print(power_wizard(power)), the following happens:
the power_wizard() function is called.
in that power_wizard() function, you call the Wizard.fire() or Wizard.wind() methods...
in both methods, you print('My power is...')...
...but you don't return any result.
in the power_wizard() function, you print the result of the Wizard.fire() or Wizard.wind() methods but, as those methods returns nothing, None is printed a first time.
the power_wizard() function returns no results either...
...thus, the outermost print(power_wizard(power)) prints a second None.
That said, your Wizard class and power_wizard() function are a bit tangled...
You could easily remove the Wizard.fire() and Wizard.wind() methods and the power_wizard() function and have:
class Wizard():
def __init__(self, name, gender, power):
self.name = name
self.gender = gender
self.power = power
player1 = Wizard("Gandalf", "male", "fire")
player2 = Wizard("Hermione", "female", "wind")
player3 = Wizard("Draco", "male", "invisibility")
for p in (player1, player2, player3):
print("My name is %s and I have %s power" % (p.name, p.power))
# My name is Gandalf and I have fire power
# My name is Hermione and I have wind power
# My name is Draco and I have invisibility power
If you want to restrict the list of Wizard powers:
class Wizard():
powers = ("fire", "wind") # this is a _class variable_ defining
# all powers available to Wizards
def __init__(self, name, gender, power):
self.name = name
self.gender = gender
if power in Wizard.powers:
self.power = power
else:
raise ValueError("%s is not a Wizard power." % (power))
player3 = Wizard("Draco", "male", "invisibility")
# ValueError: invisibility is not a Wizard power.
If you want to let wizards describe themselves:
class Wizard():
...
def __init__(self, name, gender, power):
...
def __repr__(self):
return "<% is a %s with %s power>" % (
self.name, self.__class__.__name__, self.power)
for p in (player1, player2):
print(p)
# <Gandalf is a Wizard with fire power>
# <Hermione is a Wizard with wind power>
Then, if you want to make a wizard interactively:
def make_wizard():
while True:
name = input("Enter your wizard name: ")
if name:
break
gender = input("Enter its gender: ")
choices = ", ".join(Wizard.powers)
while True:
power = input("Enter its power (%s): " % (choices))
if power in Wizard.powers:
break
return Wizard(name, gender, power)
player1 = make_wizard()
print(player1)
# Enter your wizard name:
# Enter your wizard name: Gandalf
# Enter its gender: male
# Enter its power (fire, wind): invisibility
# Enter its power (fire, wind): fire
#
# <Gandalf is a Wizard with fire power>
If you want powers to be numbered choices:
def make_wizard():
...
choices = ", ".join("%i: %s" % (i, name)
for i, name
in enumerate(Wizard.powers, start=1))
while True:
try:
choice = int(input("Enter its power (%s): " % (choices)))
# will raise ValueError if choice is not a int
power = Wizard.powers[choice-1]
# will raise IndexError if choice-1 is not
# in Wizard.powers range
except ValueError, IndexError:
continue
else:
break
return Wizard(name, gender, power)
player1 = make_wizard()
print(player1)
# Enter your wizard name:
# Enter your wizard name: Gandalf
# Enter its gender: male
# Enter its power (1: fire, 2: wind): invisibility
# Enter its power (1: fire, 2: wind): 3
# Enter its power (1: fire, 2: wind): 1
#
# <Gandalf is a Wizard with fire power>
(that was fun... ;-)

You can only run the print statement if its return is not None like this. Its a bandaid fix.
if player1.name is not None: print(player1.name)

Related

Is it possible to change the output so that "Arlene" and "Klusman" don't have an extra set of parenthesis around them?

I'm writing code for an assignment where I can't change the main. The way I have it written, it prints like in the screenshot below:
Here is my code:
import csv
class Customer:
def __init__(self, cust_id, name, lastName, companyName, address, city, state, cust_zip):
self.cust_id = cust_id
self.first_name = name
self.last_name = lastName
self.company_name = companyName
self.address = address
self.city = city
self.state = state
self.zip = cust_zip
def getFullName(self):
return(self.first_name, self.last_name)
def getFullAddress(self):
return(self.getFullName(), self.company_name, self.address, self.city, self.state, self.zip)
def get_customers():
myList = []
counter = 0
with open("customers.csv", "r") as csv_file:
reader = csv.reader(csv_file, delimiter = ",")
for row in reader:
if counter!=0:
customer1 = Customer(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7])
myList.append(customer1)
counter+=1
return myList
def find_customer_by_id(customers, cust_id):
for i in range(len(customers)):
if cust_id == customers[i].cust_id:
return customers[i]
return None
def main():
#main is fully implemented with no modification expected
print("Customer Viewer")
print()
customers = get_customers()
while True:
cust_id = input("Enter customer ID: ").strip()
print()
customer = find_customer_by_id(customers, cust_id)
if customer == None:
print("No customer with that ID.")
print()
else:
print(customer.getFullAddress())
print()
again = input("Continue? (y/n): ").lower()
print()
if again != "y":
break
print("Bye!")
if __name__ == "__main__":
main()
Why are there parenthesis and, can you get rid of them?
I tried to different approaches but nothing changed the output in the intended way

Pyhton3 Code,Class and Methods , why does the method won't return value , though it the code look ok without any problem

i try to build this code see below for the objective and advise why the method "sleep" won't return the value of self.name and the string afterwards if the cat is sleeping.. , the result only gives the second method jumpy.
# Objective:create a class cat with the following attributes:
# name , age , sex
# with the following methods: sleep :
# run only if attribute name is passed
# jumpy : if run function , sex needs to be male
class Cat:
def __init__(self, name='', age='', sex=''):
self.name = name
self.age = age
self.sex = sex
def sleep(self):
if self.name != None:
return f'{self.name} if sleeping, leave him alone!'
def jumpy(self):
if self.sex == "male":
print("the cat jumping like a maniac!!")
else:
print("this cat is lazy Garfield!!")
Mycat = Cat('miki', 5, 'female')
Mycat.sleep()
Mycat.jumpy()
this cat is lazy garfield!!

Unable to access parent class' global variable with Child class' object

I want to update the counter of available cars whenever someone rents the car. When I try to access this variable through child class, it shows an error as "UnboundLocalError: local variable 'available_cars' referenced before assignment"
PS: Still working on it, that's why I haven't completed all the method codes.
Parent Class
available_cars = 1000
class Car_rental():
def __init__(self):
pass
def display_available_cars(self):
print("Total avaialable cars for renting is:", available_cars)
def rent_hourly(self, cars_rented):
print("Our hourly rate is $100/hr.")
if cars_rented > available_cars:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
available_cars))
elif cars_rented < available_cars:
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
elif cars_rented < 0:
print ("Please provide a valid number of cars.")
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_invetory(self, cars_rented):
available_cars = available_cars - cars_rented
Child Class
from carRental import *
class Customer(Car_rental):
def init(self):
Car_rental.init(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1 or mode == 'hourly' or mode == 'Hourly':
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_invetory(cars_rented)
elif mode == 2 or mode == 'weekly' or mode == 'Weekly':
self.rent_weekly()
elif mode == 3 or mode == 'monthly' or mode == 'Monthly':
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
So if I follow your question correctly, you seem to have objectives with your code.
You want to define a Car_Rental class with a class-level variable available_cars which is initially set to 1000, and then as customers rent vehicles decrement the number of available cars.
You also want a second Customer class which inherits the Car_Rental class which implements the billing functionality.
While I am unsure of this, it seems that you would also like each of these class definitions to reside in their own separate python script file.
This is how I would address these issues.
# If desired could be placed in separate file Car_Rental.py
class Car_Rental():
available_cars = 1000
def display_available_cars(self):
print("Total avaialable cars for renting is:", Car_Rental.available_cars)
def isOkayToRent(self, nmbr):
return nmbr <= Car_Rental.available_cars
def rent_hourly(self, cars_rented):
print("Our hourly rate is $100/hr.")
if cars_rented < 0:
print ("Please provide a valid number of cars.")
else:
if self.isOkayToRent(cars_rented):
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
else:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
Car_Rental.available_cars))
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_inventory(self, nmbr):
assert nmbr > 0
Car_Rental.available_cars -= nmbr
#property
def cars(self):
return Car_Rental.available_cars
# Can be placed in second file.
#Note include this next line if classes are stored in separate files
#from Car_rental import Car_Rental
class Customer(Car_Rental):
def __init__(self):
Car_Rental.__init__(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1:
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_inventory(cars_rented)
elif mode == 2:
self.rent_weekly()
elif mode == 3:
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
To use:
c1 = Customer()
c1.cars
1000
c1.rent_cars()
Please select the mode of renting the car:
1. Hourly
2. Weekly
3. Monthly
How many cars do you wish to rent? 2
Our hourly rate is $100/hr.
Thank you for renting 2 cars from Swift car renting portal. Enjoy you ride.
To illustrate proper inventory reduction:
c1.cars yields 998

Sorting a dictionary that contains a class holding 5 variable by one of the class variables

I have a dictionary that contains keys that are made from a class containing 5 variables. I want to sort this dictionary by one of the class vars.
here is what i currently have
class Player:
def __init__(self,name,wins,losses,ties,winpercent):
self.__name = name
self.__wins = wins
self.__losses = losses
self.__ties = ties
self.__winpercent = winpercent
# mutators
def setname(self,name):
self.__name = name
def setwins(self,wins):
self.__wins = wins
def setlosses(self,losses):
self.__losses = losses
def setties(self,ties):
self.__ties = ties
def setwinpercent(self,winpercent):
self.__winpercent = winpercent
# accessors
def getname(self):
return self.__name
def getwins(self):
return self.__wins
def getlosses(self):
return self.__losses
def getties(self):
return self.__ties
def getwinpercent(self):
return self.__winpercent
def displayHighScores(self):
print("\n"," "*2,self.__name," "*(24-len(self.__name)),self.__wins)
def displayplayers(self):
print(self.__name)
I store Players like this:
def addplayer(players):
newName = input("\nEnter new Player name or 9 to quit: ")
wins = "0"
losses = "0"
ties = "0"
winpercent = "0"
if not newName:
print("\nNo name detected try again")
addplayer(players)
elif newName == '9':
print("\nReturning to Options menu")
else:
players[newName] = Player(newName,wins,losses,ties,winpercent)
saveData(players)
return players
Finally i am working on a sorted hi scores list. right now i can print my dictionary unsorted like this:
def printhiscores(players):
print("\n"," "*13,"HiScores")
print(" "*3,"Name"," "*20,"Wins")
if len(players) == 0:
print("\nNo current Players in memory.")
else:
for x in players.keys():
players[x].displayHighScores()
DisplayHighScores () being a part of the class object.
I have been reading on dictionary sorting using
OrderedDict(sorted(players.items(), key=itemgetter(1)))
but this returns edit:TypeError: '<' not supported between instances of 'Player' and 'Player'
Again I am looking to sort my dictionary of players by their win attribute and then print this new order to a high score screen. Any help would be greatly appreciated. I will post if i make any more progress on my own on this.
Your Player instances are not orderable however, as they don't implement comparison methods:
TypeError: '<' not supported between instances of 'Player' and 'Player'
If you wanted to sort them by the value returned, say, wins, then just access that information instead of just returning the value:
OrderedDict(sorted(players.items(), key=lambda kv: kv[1].getwins()))
Otherwise, give your Player class rich comparison methods; methods by which to determine when one instance should be sorted before or after another. You'll also need an equality test. It's easier to just implement support for < (lower then) and equality, then use the #functools.total_ordering decorator to provide the rest:
from functools import total_ordering
#total_ordering
class Player:
def __lt__(self, other):
if not isinstance(other, Player):
return NotImplemented
return self.__wins < other.__wins
def __eq__(self, other):
if not isinstance(other, Player):
return NotImplemented
return self.__wins == other.__wins
The above makes two Player instances equal when their wins are equal, and orders them by that attribute. You'll need to adjust this for your application as needed.

Need help understanding why my code doesn't run [python]

So my goal is to make an ordering system for a cafe with various types of coffee available. Right now I'm having trouble understanding why my code doesn't run, whenever I execute the code nothing happens and I'm not sure why
class Coffee(object):
def __init__(self,name):
self.name = name
def coffee (self, coffee_type, price):
self.coffee_type = coffee_type
self.price = price
class Order(object):
def __init__(self, name):
self.name = name
def newOrder (self, coffee_type, coffee_amount):
this.coffee_type = coffee_type
this.coffee_amount = coffee_amount
class Main(object):
def __init__ (self, name):
self.name = name
from Coffee import coffee
flat_white = coffee("1 : Flat White", 3.50)
long_black = coffee("2 : Long Black", 3.50)
cappuccino = coffee("3 : Cappuccino", 4.00)
espresso = coffee("4 : Espresso", 3.25)
latte = coffee("5 : Latte", 3.50)
Ordering = 'Y'
while Ordering == 'Y':
try:
order_coffee = int(input("What coffee would you like to order?", pprint (vars(coffee))))
break
except:
ValueError
print ("wrong input")
order_amount = input("How many would you like to order?")
if order_coffee == 1:
new_Order(flat_white, order_amount)
elif order_coffee == 2:
new_Order(long_black, order_amount)
elif order_coffee == 3:
new_Order(cappuccino, order_amount)
elif order_coffee == 4:
new_Order(espresso, order_amount)
elif order_coffee == 5:
new_Order(latte, order_amount)
else:
print ("Please enter a valid number")
break
It looks like there are several issues with your code. I will try to help you out with a couple.
First, no need for the __init__ method to accept a name. Just make the method accept the necessary information for the object:
class Coffee(object):
def __init__ (self, coffee_type, price):
self.coffee_type = coffee_type
self.price = price
class Order(object):
def __init__(self, coffee_type, coffee_amount):
self.coffee_type = coffee_type
self.coffee_amount = coffee_amount
Second, since it does not really do anything, you can replace the Main class to an execution in a conditional as is typically seen in Python programs/scripts:
if __name__ =="__main__":
... # put main code here
Next, the except as it is written will catch all exceptions making debugging more difficult. If you only want to catch the ValueError, do:
except ValueError:
print ("wrong input")
If you use the current flow (i.e. with the try except block, you should put all of the order logic within the try block as it does not make any sense outside. Also, the necessary variables needed (order_coffee) will not be defined if you have a ValueError.
Adding a break after accepting the order input will cause you to break out of the ordering loop after an order is entered. This is probably not intended behavior. No break is needed here. Ditto for after outputting "Please enter a valid number".
When creating an Order, you just need to call Order(coffee_type, coffee_amount). When you create this order, make sure to set it to a variable too. probably something like:
new_order = Order(flat_white, order_amount)
Printing "Please enter a valid number" and "wrong input" is slightly redundant. You only need one of them to print under invalid input.
Lastly, pprint will print stuff out then return None so print(..., pprint(x)) will print an extra None once it has finished pretty printing x, instead just call pprint as if it was another call to print.
-- --
Making all of these changes, you will end up with something like:
class Coffee(object):
def __init__ (self, coffee_type, price):
self.coffee_type = coffee_type
self.price = price
class Order(object):
def __init__(self, coffee_type, coffee_amount):
self.coffee_type = coffee_type
self.coffee_amount = coffee_amount
if __name__ =="__main__":
coffee_available=[Coffee("1 : Flat White", 3.50),
Coffee("2 : Long Black", 3.50),
Coffee("3 : Cappuccino", 4.00),
Coffee("4 : Espresso", 3.25),
Coffee("5 : Latte", 3.50)]
ordering = 'Y'
while ordering == 'Y':
print("Coffee Type\t\tPrice")
print("-----------\t\t-----")
for coffee in coffee_available:
print("{}\t- - -\t$ {}".format(coffee.coffee_type,coffee.price))
print()
order_coffee = int(input("What is the number of the coffee you want? "))
order_amount = input("How many would you like to order? ")
new_order = None
if order_coffee >= 1 and order_coffee <=5:
new_order = Order(coffee_available[order_coffee-1], order_amount)
else:
print ("Please enter a valid number")

Resources