Always get printed value of "None" - python

Alright so here is my code, I get the result I want but I keep getting the "None" value under it. How do I eliminate the "None" value?
n = input("What day of the week are you leaving?")
r = input("How many days will you be resting?")
def days(n):
if n == 0:
print "Sunday"
elif n == 1:
print "Monday"
elif n == 2:
print "Tuesday"
elif n == 3:
print "Wednesday"
elif n == 4:
print "Thrusday"
elif n == 5:
print "Friday"
elif n == 6:
print "Saturday"
elif n >= 7:
print days(n%7)
print days(n+r)

This should do the trick:
days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
print days[(n+r) % 7]

days never returns anything, so it implicitly returns None. Change all of the print statements in days to return statements:
def days(n):
if n == 0:
return "Sunday"
elif n == 1:
return "Monday"
elif n == 2:
return "Tuesday"
elif n == 3:
return "Wednesday"
elif n == 4:
return "Thrusday"
elif n == 5:
return "Friday"
elif n == 6:
return "Saturday"
elif n >= 7:
return days(n % 7)

Change all the print statements in your days(n) function to return instead.

You print in function days and print result from function days.
Because of function days returns nothing it prints None.

Related

how i break the while loop in func "loop of user"?

I need to break the while loop, I try to do it with status statment and it didn't work to me.
Any suggestions what are the easiest ways to break a while loop?
This is my code:
def loop_of_user(my_details):
"""_summary_
the function do a variety of actions on the the variable my_detailes
:my_details (dict): dictionary of detailes on mariha
"""
num = int(input())
if num == 1:
print(my_details["first_name"])
elif num == 2:
print(my_details["birth_date"][3:5])
elif num == 3:
print(len(my_details["hobbies"]))
elif num == 4:
print(my_details["hobbies"][-1])
elif num == 5:
my_details["hobbies"].append("cooking")
print(my_details["hobbies"])
elif num == 6:
print(tuple_birth_date(my_details["birth_date"]))
elif num == 7:
my_details ["age"] = calculate_age(my_details["birth_date"])
print(my_details["age"])
else:
return "break"
def main():
mariah_details = {"first_name" : "mariah", "last_name" : "carey", "birth_date" : "27.03.1970", "hobbies" : ["sing", "compose", "act"]}
status = ""
while status != "break":
loop_of_user(mariah_details)
if __name__ == "__main__":
main()
The I try to use in satatus like you see and write "break" in the else satement and it not working, it still in the loop and won't break.
I will love some help here.
You can put the while loop inside the function loop_of_user instead and call the function loop_of_user() explicitly.
def loop_of_user(my_details):
"""_summary_
the function do a variety of actions on the the variable my_detailes
:my_details (dict): dictionary of detailes on mariha
"""
while True:
num = int(input())
if num == 1:
print(my_details["first_name"])
elif num == 2:
print(my_details["birth_date"][3:5])
elif num == 3:
print(len(my_details["hobbies"]))
elif num == 4:
print(my_details["hobbies"][-1])
elif num == 5:
my_details["hobbies"].append("cooking")
print(my_details["hobbies"])
elif num == 6:
print(tuple_birth_date(my_details["birth_date"]))
elif num == 7:
my_details["age"] = calculate_age(my_details["birth_date"])
print(my_details["age"])
else:
break
def main():
mariah_details = {"first_name": "mariah", "last_name": "carey", "birth_date": "27.03.1970",
"hobbies": ["sing", "compose", "act"]}
loop_of_user(mariah_details)
if __name__ == "__main__":
main()

Enumerate over list of integers and access list value by index [duplicate]

This question already has answers here:
Error: 'int' object is not subscriptable - Python
(10 answers)
Roman Numerals to Integers Converter with Python using a dictionary
(5 answers)
Basic program to convert integer to Roman numerals?
(36 answers)
Closed 1 year ago.
I'm trying to solve a LeetCode problem and am not sure why what I have done does not work. Moreso than simply getting a working solution, I am trying to figure out why this approach does not work. The error that I am getting is:
TypeError: 'int' object is not subscriptable
if value[0] == 0:
Line 22 in intToRoman (Solution.py)
ret = Solution().intToRoman(param_1)
Line 123 in _driver (Solution.py)
_driver()
Line 134 in <module> (Solution.py)
So, it seems to be complaining about the section where I enumerate my list and try to access the value of the index. Here is my code:
class Solution:
def intToRoman(self, num: int) -> str:
answer = ""
# Calculate length of num
num = str(num)
numLength = len(num)
# Convert num to list of individual digits and reverse
numArray = [int(digit) for digit in num]
numArray.reverse()
# Build Roman Numeral
firstDigit = ""
secondDigit = ""
thirdDigit = ""
fourthDigit = ""
for count, value in enumerate(numArray):
if numLength >= 1:
if value[0] == 0:
firstDigit = None
if value[0] == 1:
firstDigit = "I"
if value[0] == 2:
firstDigit = "II"
if value[0] == 3:
firstDigit = "III"
if value[0] == 4:
firstDigit = "IV"
if value[0] == 5:
firstDigit = "V"
if value[0] == 6:
firstDigit = "VI"
if value[0] == 7:
firstDigit = "VII"
if value[0] == 8:
firstDigit = "VIII"
if value[0] == 9:
firstDigit = "IX"
if numLength >= 2:
if value[1] == 0:
secondDigit = None
if value[1] == 1:
secondDigit = "X"
if value[1] == 2:
secondDigit = "XX"
if value[1] == 3:
secondDigit = "XXX"
if value[1] == 4:
secondDigit = "XL"
if value[1] == 5:
secondDigit = "L"
if value[1] == 6:
secondDigit = "LX"
if value[1] == 7:
secondDigit = "LXX"
if value[1] == 8:
secondDigit = "LXXX"
if value[1] == 9:
secondDigit = "XC"
if numLength >= 3:
if value[2] == 0:
thirdDigit = None
if value[2] == 1:
thirdDigit = "C"
if value[2] == 2:
thirdDigit = "CC"
if value[2] == 3:
thirdDigit = "CCC"
if value[2] == 4:
thirdDigit = "CD"
if value[2] == 5:
thirdDigit = "D"
if value[2] == 6:
thirdDigit = "DC"
if value[2] == 7:
thirdDigit = "DCC"
if value[2] == 8:
thirdDigit = "DCCC"
if value[2] == 9:
thirdDigit = "CM"
if numLength == 4:
if value[3] == 0:
fourthDigit = None
if value[3] == 1:
fourthDigit = "M"
if value[3] == 2:
fourthDigit = "MM"
if value[3] == 3:
fourthDigit = "MMM"
answer = (fourthDigit + thirdDigit + secondDigit + firstDigit)
if numLength == 3:
answer = (thirdDigit + secondDigit + firstDigit)
if numLength == 2:
answer = (secondDigit + firstDigit)
if numLength == 1:
answer = (firstDigit)
return(answer)
The LeetCode problem is:
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Any comments about my general approach to solving the problem are welcome as well.
Note regarding the question being closed as duplicate -- none of the linked articles deal with Python enumerate() or answer my question, as far as I can tell.

"<function funcname at address>" printed instead of function's result

I'm new at python and I'm trying to create this game using words picked at random based on words picked
from a previous function and whenever I run the script it keeps spitting out this weird code that I have no idea what it is. What I want instead of spitting out this weird code I want it to be a word. Do you have any tips of how to make it happen?
from random import randint
import time
from line import bloodline
bloodline()
if bloodline == 'Shadow':
spower = randint(1,6)
if spower == 1:
infinity = 'shade'
elif spower == 2:
infinity = 'decay'
elif spower == 3:
infinity == 'black'
elif spower == 4:
infinity = 'vampiric'
elif spower == 5:
infinity = 'eclispe'
print (infinity)
elif bloodline == 'Verglas':
ipower = randint(1,6)
if ipower == 1:
infinity = 'frozen'
elif ipower == 2:
infinity = 'artic'
elif ipower == 3:
inifinity ='glacier'
elif ipower == 4:
infinity = 'white'
elif ipower == 5:
infinity = 'biting'
print (infinity)
elif bloodline == 'Flame':
fpower = randint(1,6)
if fpower == 1:
infinity = 'blazing'
elif fpower == 2:
infinity = 'searing'
elif fpower == 3:
infinity = 'burning'
elif fpower == 4:
infinity = 'sparking'
elif fpower == 5:
infinity = 'scorching'
print (infinity)
output:
<function bloodline at 0x043D1E88>#this right here is what i'm trying to get rid off
code from line file:
from random import randint
def bloodline():
bloodline = randint(1,101)
if bloodline >= 61:
bloodline = 'Verglas'
elif bloodline >= 20:
bloodline = 'Flame'
elif bloodline >= 0:
bloodline = 'Shadow'
print (bloodline)
bloodline is a function, not a variable containing the value that it returns. You need to assign the function call to a variable, then use that in the rest of the code.
from random import randint
import time
from line import bloodline
bl = bloodline()
if bl == 'Shadow':
spower = randint(1,6)
if spower == 1:
infinity = 'shade'
elif spower == 2:
infinity = 'decay'
elif spower == 3:
infinity == 'black'
elif spower == 4:
infinity = 'vampiric'
elif spower == 5:
infinity = 'eclispe'
print (infinity)
elif bl == 'Verglas':
ipower = randint(1,6)
if ipower == 1:
infinity = 'frozen'
elif ipower == 2:
infinity = 'artic'
elif ipower == 3:
inifinity ='glacier'
elif ipower == 4:
infinity = 'white'
elif ipower == 5:
infinity = 'biting'
print (infinity)
elif bl == 'Flame':
fpower = randint(1,6)
if fpower == 1:
infinity = 'blazing'
elif fpower == 2:
infinity = 'searing'
elif fpower == 3:
infinity = 'burning'
elif fpower == 4:
infinity = 'sparking'
elif fpower == 5:
infinity = 'scorching'
print (infinity)
And in the line file, you need to return a value from the function.
from random import randint
def bloodline():
result = randint(1,101)
if result >= 61:
result = 'Verglas'
elif result >= 20:
result = 'Flame'
elif result >= 0:
result = 'Shadow'
return result
Don't use the same name bloodline for the function name and a variable.
The last line, print (bloodline), is printing out the function itself, not it's return value, since you're not calling it. However, if you were to call it and print it out, it would print out None, since the function bloodline doesn't return anything.
The simplest way to fix this is to remove print (bloodline) altogether, or returning the value you want to use.
Another thing I noticed is that the variable bloodline could be interpreted by Python to be the function itself, so it may rebind the function to a value.
Within the bloodline() function you should add a return(bloodline) at the end, as well as removing the print(bloodline) after defining the function.
You should also add return statement at the end of the function definition

python: I have this code (below) but it prints in reverse, how can I reverse it

How can I reverse the output?
def dectohex(num):
z = int(0)
y = int(0)
if num > 0:
z = num // 16
y = int((num % 16))
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
elif y == 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9:
print(y, end = "")
dectohex(z)
inp = int(input("Enter number "))
dectohex(inp)
Calling the recursive function earlier will reverse the output:
def dectohex(num):
if num > 0:
z = num // 16
dectohex(z)
y = num % 16
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
else:
print(y, end = "")
Note that I also made some other optimization to the function. Notably, I removed unnecessary initialization, casting and simplified the if-chain, since the number y is known to be an integer with 0 <= y <= 15.

Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'

I don't understand this error OR what it means. I will paste my code underneath, but I don't think it is really relevant; I just want to understand this error.
It's just a bit of code to add up the letters in all numbers 1 - 1000 (inclusive)
def number_translator(x):
if x == 1:
return 3
elif x == 2:
return 3
elif x == 3:
return 5
elif x == 4:
return 4
elif x == 5:
return 4
elif x == 6:
return 3
elif x == 7:
return 5
elif x == 8:
return 5
elif x == 9:
return 4
elif x == 10:
return 3
elif x == 11:
return 6
elif x == 12:
return 6
elif x == 14:
return 8
elif x == 15:
return 7
elif x == 16:
return 7
elif x == 17:
return 9
elif x == 18:
return 8
elif x == 19:
return 8
elif x == 20:
return 6
elif x == 30:
return 6
elif x == 40:
return 5
elif x == 50:
return 5
elif x == 60:
return 5
elif x == 70:
return 7
elif x == 80:
return 6
elif x == 90:
return 6
count = 0
for element in range(1, 1001):
if element < 21:
count += number_translator(element) # for numbers 1 - 20
elif 20 < element < 100:
count += number_translator(int(str(element)[0]) * 10) + number_translator(int(str(element)[1])) # for numbers 21 through 100
elif element % 100 == 0 and element != 1000:
count += number_translator(int(str(element)[0])) + 7 # for numbers divisible by 100, but not 1000
elif element == 1000:
count += 11 # just for 1000
elif element % 100 < 20:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1:3])) # now I add in numbers like 101 - 120, 201 - 220, etc.
else:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1]) * 10) + number_translator(int(str(element)[2])) # now the rest( 121, 122, 123, 225, 256, 984, etc.)
print(count)
When none of the if test in number_translator() evaluate to true, the function returns None. The error message is the consequence of that.
Whenever you see an error that include 'NoneType' that means that you have an operand or an object that is None when you were expecting something else.
In your giant elif chain, you skipped 13. You might want to throw an error if you hit the end of the chain without returning anything, to catch numbers you missed and incorrect calls of the function:
...
elif x == 90:
return 6
else:
raise ValueError(x)
I got a similar error with '/' operand while processing images. I discovered the folder included a text file created by the 'XnView' image viewer. So, this kind of error occurs when some object is not the kind of object expected.

Categories