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

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

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.

Getting RecursionError: maximum recursion depth exceeded in comparison

creating a minesweeper game in pygame and i am getting a recursion error when running my code. how do i mitigate this? This is the code I have that checks to see if the clicked grid square is empty and if it is then it reveals that grid square as well as all the adjacent squares. the section that is getting this error is below:
def reveal_empty(rn,c, grid, revealed,box):
if grid[rn][c] != '0' and grid[rn][c] != '*':
revealed[rn][c] = True
if grid[rn][c] == '0':
revealed[rn][c] = True
# change row above
if rn-1 > -1:
r = grid[rn-1]
if c-1 > -1:
if not r[c-1] == '*':
revealed[rn-1][c-1] = True
reveal_empty(rn-1,c-1, grid, revealed,box)
if not r[c] == '*':
revealed[rn-1][c] = True
reveal_empty(rn-1,c, grid, revealed,box)
if c+1 < 10:
if not r[c+1] == '*':
revealed[rn-1][c+1] = True
reveal_empty(rn-1,c+1, grid, revealed,box)
#change same row
r = grid[rn]
if c-1 > -1:
if not r[c-1] == '*':
revealed[rn][c-1] + True
reveal_empty(rn,c-1, grid, revealed,box)
if c+1 < 10:
if not r[c+1] == '*':
revealed[rn][c+1] = True
reveal_empty(rn,c+1, grid, revealed,box)
#change row below
if rn+1 < 11:
r = grid[rn + 1]
if c-1 > -1:
if not r[c-1] == '*':
revealed[rn+1][c-1] = True
reveal_empty(rn+1,c-1, grid, revealed,box)
if not r[c] == '*':
revealed[rn+1][c] = True
reveal_empty(rn+1,c, grid, revealed,box)
if c+1 < 11:
if not r[c+1] == '*':
revealed[rn+1][c+1] = True
reveal_empty(rn+1,c+1, grid, revealed,box)
I guess you have this problem because there is no quick exit-clause for your recursive function. I suspect that because you don't check to see if the cell is already revealed ( revealed[row][col] == True ), then it never exits - it keeps recursing for ones already half-done in the processing queue (stack).
Maybe a quick check at the beginning of the function will fix it:
def reveal_empty( row, col, grid, revealed, box ):
if ( revealed[row][col] == False ):
# do recursive check else here!
else:
print("Cell[%d][%d] is already revealed" % ( row, col ) )
I figured it out. I had to add a check to each step of the recursion to check if the value has been revealed yet. see below:
# change row above
if rn-1 > -1:
r = grid[rn-1]
if c-1 >= -1:
if not r[c-1] == '*' and revealed[rn-1][c-1] == False:
revealed[rn-1][c-1] = True
if grid[rn-1][c-1] == '0':
reveal_empty(rn-1,c-1, grid, revealed,box)
if not r[c] == '*' and revealed[rn-1][c] == False:
revealed[rn-1][c] = True
if grid[rn-1][c] == '0':
reveal_empty(rn-1,c, grid, revealed,box)
if c+1 < 10:
if not r[c+1] == '*' and revealed[rn-1][c+1] == False:
revealed[rn-1][c+1] = True
if grid[rn-1][c+1] == '0':
reveal_empty(rn-1,c+1, grid, revealed,box)
#change same row
r = grid[rn]
if c-1 > -1:
if not r[c-1] == '*' and revealed[rn][c-1] == False:
revealed[rn][c-1] + True
if grid[rn][c-1] == '0':
reveal_empty(rn,c-1, grid, revealed,box)
if c+1 < 10:
if not r[c+1] == '*' and revealed[rn][c+1] == False:
revealed[rn][c+1] = True
if grid[rn][c+1] == '0':
reveal_empty(rn,c+1, grid, revealed,box)
#change row below
if rn+1 < 11:
r = grid[rn + 1]
if c-1 > -1:
if not r[c-1] == '*' and revealed[rn+1][c-1] == False:
revealed[rn+1][c-1] = True
if grid[rn+1][c-1] == '0':
reveal_empty(rn+1,c-1, grid, revealed,box)
if not r[c] == '*' and revealed[rn+1][c] == False:
revealed[rn+1][c] = True
if grid[rn+1][c] == '0':
reveal_empty(rn+1,c, grid, revealed,box)
if c+1 < 11:
if not r[c+1] == '*' and revealed[rn+1][c+1] == False:
revealed[rn+1][c+1] = True
if grid[rn+1][c+1] == '0':
reveal_empty(rn+1,c+1, grid, revealed,box)

Deleting a Label widget created in a For Loop (Tkinter)

This code is part of a program I'm developing. It uses CSV to read from a file. This part of the code is supposed to compare the input date to the dates in the CSV file and output any matching dates and the information they carry. This is the CSV data: CSV file
The problem is, when I input another date in the filed, I want it to delete any labels created in the for loop of the code. The deleteResults function only deletes the last set of labels created. Is there a way to delete via another function all the labels created in the for loop, not mattering the amount of labels created with the for loop? Thanks.
#PLANNER
from tkinter import *
import tkinter
from csv import*
import csv
colW=['#000000','#f9f4bd','#fff200','black','#2378c8','#ed1c24']
colSch=colW
accessDayPage=False
results=False
existingEvents=[]
eventsFile=open("dayFile.csv","r+")
evImport=csv.reader(eventsFile)
for row in evImport:
existingEvents.append(row)
def deleteResults():
global dateLabel,descriptionLabel,titleLabel,results
if results==True:
dateLabel.destroy()
descriptionLabel.destroy()
titleLabel.destroy()
evTypeLabel.destroy()
results=False
def destroyaccessDay():
global accessDayPage
if accessDayPage==True:
accessD.destroy()
accessM.destroy()
accessY.destroy()
slash.destroy()
slash1.destroy()
accessButton.destroy()
accessDayPage=False
def go():
global dayAcc,existingEvents,dateLabel,descriptionLabel,titleLabel,dayAccFORSEARCH,eventsCreated,evTypeLabel,results
deleteResults()
lineforevent=93
leDay="0"
leMon="404"
if accessD.get()=="01":
leDay="1"
elif accessD.get()=="02":
leDay="2"
elif accessD.get()=="03":
leDay="3"
elif accessD.get()=="04":
leDay="4"
elif accessD.get()=="05":
leDay="5"
elif accessD.get()=="06":
leDay="6"
elif accessD.get()=="07":
leDay="7"
elif accessD.get()=="08":
leDay="8"
elif accessD.get()=="09":
leDay="9"
elif accessD.get()=="1":
leDay="1"
elif accessD.get()=="2":
leDay="2"
elif accessD.get()=="3":
leDay="3"
elif accessD.get()=="4":
leDay="4"
elif accessD.get()=="5":
leDay="5"
elif accessD.get()=="6":
leDay="6"
elif accessD.get()=="7":
leDay="7"
elif accessD.get()=="8":
leDay="8"
elif accessD.get()=="9":
leDay="9"
elif accessD.get()=="10":
leDay="10"
elif accessD.get()=="11":
leDay="11"
elif accessD.get()=="12":
leDay="12"
elif accessD.get()=="13":
leDay="13"
elif accessD.get()=="14":
leDay="14"
elif accessD.get()=="15":
leDay="15"
elif accessD.get()=="16":
leDay="16"
elif accessD.get()=="17":
leDay="17"
elif accessD.get()=="18":
leDay="18"
elif accessD.get()=="19":
leDay="19"
elif accessD.get()=="20":
leDay="20"
elif accessD.get()=="21":
leDay="21"
elif accessD.get()=="22":
leDay="22"
elif accessD.get()=="23":
leDay="23"
elif accessD.get()=="24":
leDay="24"
elif accessD.get()=="25":
leDay="25"
elif accessD.get()=="26":
leDay="26"
elif accessD.get()=="27":
leDay="27"
elif accessD.get()=="28":
leDay="28"
elif accessD.get()=="29":
leDay="29"
elif accessD.get()=="30":
leDay="30"
elif accessD.get()=="31":
leDay="31"
if accessM.get()=="1":
leMon="January"
elif accessM.get()=="01":
leMon="January"
elif accessM.get()=="2":
leMon="February"
elif accessM.get()=="02":
leMon="February"
elif accessM.get()=="3":
leMon="March"
elif accessM.get()=="03":
leMon="March"
elif accessM.get()=="4":
leMon="April"
elif accessM.get()=="04":
leMon="April"
elif accessM.get()=="5":
leMon="May"
elif accessM.get()=="05":
leMon="May"
elif accessM.get()=="6":
leMon="June"
elif accessM.get()=="06":
leMon="June"
elif accessM.get()=="7":
leMon="July"
elif accessM.get()=="07":
leMon="July"
elif accessM.get()=="8":
leMon="August"
elif accessM.get()=="08":
leMon="August"
elif accessM.get()=="9":
leMon="September"
elif accessM.get()=="09":
leMon="September"
elif accessM.get()=="10":
leMon="October"
elif accessM.get()=="10":
leMon="October"
elif accessM.get()=="11":
leMon="November"
elif accessM.get()=="11":
leMon="November"
elif accessM.get()=="12":
leMon="December"
elif accessM.get()=="12":
leMon="December"
dayAcc=str(leDay+leMon+accessY.get())
dayAccFORSEARCH=str(dayAcc+"'")
for i in existingEvents:
global results
if i[0]==dayAccFORSEARCH:
deleteResults()
titleLabel=tkinter.Label(main, text=i[1],fg=colSch[3],bg=colSch[1],font=("DFKai-SB",9,"bold"))
titleLabel.place(x=140,y=lineforevent)
evTypeLabel=tkinter.Label(main, text=i[2],fg=colSch[4],bg=colSch[0],font=("Consolas",9))
evTypeLabel.place(x=320,y=lineforevent)
descriptionLabel=tkinter.Label(main,text=i[3],fg=colSch[5],bg=colSch[1],font=("Garamond",9))
descriptionLabel.place(x=420,y=lineforevent)
lineforevent+=35
dateLabel=tkinter.Label(main, text=dayAcc,fg=colSch[4],bg=colSch[0],font=("Consolas",12))
dateLabel.place(x=140,y=35)
results=True
def accessDay():
destroyaccessDay()
deleteResults()
global accessD,accessM,accessY,slash,slash1,accessButton,accessDayPage
accessD=tkinter.Entry(main,width=2)
accessD.place(x=140, y=8)
slash=tkinter.Label(main,text="/",fg=colSch[4],bg=colSch[0],font=("14"))
slash.place(x=160,y=8)
accessM=tkinter.Entry(main,width=2)
accessM.place(x=175, y=8)
slash1=tkinter.Label(main,text="/",fg=colSch[4],bg=colSch[0],font=("14"))
slash1.place(x=195,y=8)
accessY=tkinter.Entry(main,width=4)
accessY.place(x=210, y=8)
accessButton=tkinter.Button(main, text="Go",bg=colSch[2],fg=colSch[4],width=6,height=1, font=("Copperplate Gothic Light","9"),command=go)##
accessButton.place(x=245,y=8)
accessDayPage=True
main=tkinter.Tk()
main.configure(bg=colSch[0])
main.geometry('780x375')
w=Canvas(main,width=780,height=375)
w.pack()
rect1=w.create_rectangle(0,0,775,375,fill=colSch[0])
rect2=w.create_rectangle(0,0,120,375,fill=colSch[1])
accessDay=tkinter.Button(main,text="Access Day",bg=colSch[2],fg=colSch[4],width=15,height=3,relief=FLAT, font=("Bernard MT Condensed","11"),command=accessDay)
accessDay.place(x=5,y=5)
main.mainloop()
You'll find that the method is similar to this post here where the example I gave uses dynamicly generated widgets. You'll want to look at .destoy() instead of .configure()
Side notes:
I would suggest use in for your if statements to shorten the code since you are performing the same operation for a different instance.
if accessM.get() in {"1", "01"}:
leMon="January"
Alternatively as Bryan Oakley suggested. Using a dictionary. I believe it would be something like this.
date_values = {"01" : ("January", range(1, 31)),
"02" : ("February", range(1, 30)), # Check for leap years
.....}
leMon = date_values[accessM.get()][0]
d = int(accessD.get())
# Checks if day is in month range
leDay = str(d) if d in date_values[accessM.get()][1] else None

Always get printed value of "None"

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.

Categories