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
Related
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")
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")
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')
Hi guys check out this code.
code_tip = "code a conditional decision like you would say it"
location = work_tip.find("i")
print(location)
print (code_tip[location])
well I expect the find object to return the index of "i" which in this case is 11,and store in a variable location and the index is used to print the word "i" from the code-tip string, well it turns out that the find object rather returns a value of 10 instead of 11, and print outs "d" instead of "i".
Is there any error in this code cause i don't know why.
below is the output of the code
code_tip = "code a conditional decision like you would say it"
location = work_tip.find("i")
print(location)
print (code_tip[location])
#Output of the code.
10
d
can anyone explain what's going on.
You're running find on work_tip, not on code_tip. If you run it on the right variable you'll get the expected result:
>>> code_tip = "code a conditional decision like you would say it"
>>> location = code_tip.find("i")
>>> print(location)
11
>>> print (code_tip[location])
i
It seems to me that you mixed code_tip and work_tip at the second line. Try this:
code_tip = "code a conditional decision like you would say it"
location = code_tip.find("i")
print(location)
print (code_tip[location])
first of always use '' and not "" for Strings and second you should look in your code_tip not work_tip, here is how you can find it
Showing the answer
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.