I'm getting ''none'' instead of result - python

Hi im a beginner python user. I coded a thing just for practice but im getting ''none'' instead of the result. code is like this :
code
i think im using str function wrong but i dont know how to fix it.
thanks for your help

See, when you don't use the return keyword, a function returns None. So when you print a function which returns None, Then None will be printing.
So, use the return keyword.
def work_time(mathematic, chemistry, biology):
print(mathematic*2 + chemistry*1 + biology*(0.5))
return mathematic*2 + chemistry*1 + biology*(0.5)
print("I will study " + str(work_time(1, 2,3) ) + " hours today")

I can't see an image or code
but things that can result to none can be:-
1.Not returning a blue from function
2.Keeping a boolean inactive
3.Not defining codes

none is telling you that the function is not returning a value so you must use the return keyword

you should use return instead of print
def work_time(mathematic, chemistry, biology):
return mathematic*2 + chemistry*1 + biology*(0.5)
print("I will study " + str(work_time(1, 2,3) ) + " hours today")

Related

Value of string to be a trigger

I'm trying to get my code to get a string of data from a sensor, and then do something when it reads a specific value.
The following code is how I'm receiving the data now (this is just a test) the function is called earlier in the code at the time where I want it to be called.
def gettemperature(self)
temp = self.board.temp_sensor
print("Temperature is " + str(round(temp)) + " degrees.")
This code works, and returns the temperature value rounded to the terminal, but how could I, instead of printing the value to the terminal, make it so when that string of rounded value is say, 200 degrees, then it prints the temperature value? instead of printing it every 2 seconds (the frequency of the data being received, as per another part of the code)
Using something like
if temp = 200
then print(blahblah)
in short, the above code is what I'm trying to do. If the temp equals a certain value, then something else will happen.
That last code doesn't work. I'm pretty new to coding, so I'm assuming I'm either not going about this the right way, or the syntax of how I'm going about trying to get that value to do something isn't correct (obviously)
Thanks for any help! I'm surprised I got this far, haha.
It would be better if your function gettemperature would return something and then print the result in the condition:
def gettemperature()
temp = board.temp_sensor
return temp
temp = gettemperature()
if temp == 200:
print("Temperature is " + str(round(temp)) + " degrees.")
Before using stackoverflow, I'd recommend learning all this stuff from some basic course, as you'll get to learn the stuff yourself rather then get the answer from someone else.
Try learning conditional statements.
what you want is, to put a conditional statement which triggers if temperature is greater than 200.
If the temp is always a number in string data type, you can use the below code.
def gettemperature(self):
temp = self.board.temp_sensor
print("Temperature is " + str(round(temp)) + " degrees.")
temp=int(temp) #typecasting string datatype to integer
if temp == 200:
print("Temperature is high")

why python function execution gives me nothing

I'm trying some functions and when running it it gives me nothing
def formatted_city(city_name, country_name):
formatted_name = city_name + "' " + country_name
return formatted_name.title()
formatted_city('cairo', 'egypt')
expected : "Cairo' Egypt"
actual result: nothing happens
and when testing it it also gives me nothing
You need to print it:
print(formatted_city('cairo', 'egypt'))
Output:
Cairo' Egypt
It depends on the console. If you ran this in google collab (which I did) it will print out the two cities. However in most guis you should not expect anything to print because formatted_name.title() is not saved to any variable. You could do
x = formatted_city('cairo', 'egypt')
print(x)
which would print out what you want

If and Else statement Variable Question in Python

I am still pretty new to Python and learning! I searched around and some postings seem too complex for me at this time. Wondering why the car_brandp below is not joining with "and quite expensive" after the else function initiates? The first else line prints fine but it seems like I can't put that message as a variable?
I got the None Type error
car_brand =input ("What is the best car brand? ")
if car_brand == ("Range Rover"):
print (car_brand + " is the best car brand ever!")
else:
car_brandp = print (car_brand + " is just personal taste..")
print (car_brandp + " and quite expensive...")
This line:
car_brandp = print (car_brand + " is just personal taste..")
is suppose to be:
car_brandp = (car_brand + " is just personal taste..")
"print" is a procedure to display something in the console. A procedure differs from a function as it is not meant to return something of value, but rather perform something as a side effect (it will do something useful but you cannot interact with it). You may not assign the return value of the print function as it is meaningless.
Since you are still new to Python, it is a good idea to learn the proper habits early. In particular, PEP8 contains valuable information on style and conventions that most Python developers follow. Such recommendations are optional, but when followed, they help other developers understand your code better.
car_brand = input("What is the best car brand? ")
if car_brand == "Range Rover":
msg = car_brand + " is the best car brand ever!"
else:
msg = car_brand + " is just personal taste.."
msg += " and quite expensive..."
print(msg)
print() is like a void function in other languages. So, it does something, but it returns nothing (None, in python, like null in other languages).
So, in your next line, you are trying to add
None + "personal taste"
and you get an error because addition of string and None is not defined
So, you options are
consecutive prints
concatenate strings eg print( str(brand) + "personal taste")
formatted strings eg print( f'{brand} personal taste')

Django: Why am I getting a Value Error?

The variables are defined before the if statement.
if (len(Item.objects.filter(owner=request.session['id'])) > 0):
for x in Item.objects.filter(owner=request.session['id']):
if (x.item == forageitem):
x.amount = x.amount + 1
x.save()
messages.success(request, "You found a " + forageitem +".")
return redirect("/dashboard")
else:
continue
The function passes through the if statement just fine, but if the item in question is not found, it will not create a new object in the Item class, instead it will give me an error.
For some reason, I am getting a ValueError (didn't get an HttpResponse. Instead it reutunred None.) at this part of my code:
else:
Item.objects.create(item=forageitem, owner=User.objects.get(id = request.session['id']), description=item[forageitem], amount=1)
messages.success(request, "You found a " + forageitem +".")
return redirect("/dashboard")
I am almost certain I didn't change anything here, it was working before.
Ideas, anyone? Please help.
EDIT: The problem turned out to be int the logic.
I erased the else: statement and made it into a code that runs if it passes through the if: statement without finding anything.

Name error when calling function asking for input from user

def trip_cost(city,days,spending_money):
days = input("Enter amount of days for car hire")
city = input("City name")
days = input("Nights staying")
spending_money = input("Spending money")
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
print((trip_cost(city,days,spending_money)))
I keep getting an error saying that city is not defined. I am new to Python so I am sorry if this is question that is easy to answer. All the variables already have their set functions. Here is city's one just in case it helps
def plane_ride_cost(city):
if city=="Charlotte":
return 183
elif city=="Tampa":
return 220
elif city=="Pittsburgh":
return 222
elif city=="Los Angeles":
return 475
else:
return input("Please try again")
Also this is an altered code from Code Academy
def trip_cost():
days = int(input("Enter amount of days for car hire"))
city = input("City name")
nights = int(input("Nights staying"))
spending_money = int(input("Spending money"))
return hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
print(trip_cost())
Get rid of the parameters; you do not need them since you will get it from user input via the input() function.
I used the int function to convert the input of numbers which are strings to integers.
In your print((trip_cost(city,days,spending_money))) call, where is city coming from? It looks like it is outside any of the functions so you need to declare it somewhere as something. And even when you do so, you will get the same error for days and spending_money. These need to be declared before they are printed.
Either that or actually pass in values to your trip_cost call in the print statement :)
Also, looking at the code more closely, it looks like your trip_cost method does not even need any arguments. you are creating the variables when asking for input so it looks like they are redundant as parameters.

Categories