Help with define a def function
 Help with define a def function Omer_ Programmer named Tim Posts: 6 Threads: 5 Joined: Jul 2020 Reputation: 0 Sep-20-2020, 06:25 PM Hi, I have this code: ```ordinal = ['2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th'] ratio = [1.222223, 1.25, 1.33,4.3,1.23,2.323] i = 0 freq=int(input("Enter a fundamental frequency: ")) print(f"the fundumental frequency is: {freq}") while i < len(ratio): nfreq=freq*(ratio[i]) print(f"{ordinal[i]} note is: " + str(round(nfreq,3))) i += 1 ```for hours now I try to convert the above code to a def function so I can use it with gui interface(or call it when ever I need it) I just can't get it. can someone help me with convert the above? I cant figure what I'm missing. Thanks Reply ndc85430 Da Bishop Posts: 1,147 Threads: 0 Joined: Apr 2017 Reputation: 76 Sep-20-2020, 06:29 PM What have you tried? Also the term is just "function"; `def` is just the keyword used to, well, define a function. Reply deanhystad Da Bishop Posts: 1,254 Threads: 5 Joined: Feb 2020 Reputation: 132 Sep-20-2020, 06:48 PM My first impulse is this: ```def weird_function(): ordinal = ['2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th'] ratio = [1.222223, 1.25, 1.33,4.3,1.23,2.323] freq=int(input("Enter a fundamental frequency: ")) print(f"the fundumental frequency is: {freq}") for r, ord in zip(ratio, ordinal): print(f"{ord} note is: " + str(round(freq*r,3))) weird_function()```But as the name implies, I think this is a weird function. It is unusual for a function to contain "input" and "print" commands. Maybe this makes perfect sense in this case, but you mentioned using this in a gui interface and "input" and "print" don't work for those. I would be inclined to write this as a function that takes a frequency and returns a list of notes. ```def weird_function(note): ratios = [1.0, 1.222223, 1.25, 1.33,4.3,1.23,2.323] return [note * ratio for ratio in ratios] freq = int(input("Enter a fundamental frequency: ")) print(*weird_function(freq)) ``` Reply Omer_ Programmer named Tim Posts: 6 Threads: 5 Joined: Jul 2020 Reputation: 0 Sep-20-2020, 06:59 PM (This post was last modified: Sep-20-2020, 07:12 PM by Omer_.) ```import tkinter as tk HEIGHT = 500 WIDTH = 600 def multiple(fund): ratio = [1.222223, 1.25, 1.33, 4.3, 1.23, 2.323] i = 0 for i in range (len(ratio)): nfreq = fund * (ratio[i]) print(f"{i} note is: " + str(round(nfreq, 3))) i += 1 scale= multiple(int(input("Enter a fund: "))) print(scale) root = tk.Tk() canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH) canvas.pack() frame = tk.Frame(root, bg='#80c1ff', bd=5) frame.place(relx=0.5, rely=0.3, relwidth=0.5, relheight=0.1, anchor='n') entry = tk.Entry(frame, font=40) entry.place(relwidth=0.65, relheight=1) button = tk.Button(frame, text="Get scale", font=40, command=lambda:multiple(entry.get())) button.place(relx=0.7, relheight=1, relwidth=0.3) root.mainloop() ```this is one try, (Sep-20-2020, 06:48 PM)deanhystad Wrote: But as the name implies, I think this is a weird function. It is unusual for a function to contain "input" and "print" commands. Maybe this makes perfect sense in this case, but you mentioned using this in a gui interface and "input" and "print" don't work for those. I would be inclined to write this as a function that takes a frequency and returns a list of notes. ```def weird_function(note): ratios = [1.0, 1.222223, 1.25, 1.33,4.3,1.23,2.323] return [note * ratio for ratio in ratios] freq = int(input("Enter a fundamental frequency: ")) print(*weird_function(freq)) ``` Thanks for this. So I try to continue this method like that: ```import tkinter as tk HEIGHT = 500 WIDTH = 600 def weird_function(note): ratios = [1.0, 1.222223, 1.25, 1.33, 4.3, 1.23, 2.323] return [note * ratio for ratio in ratios] freq = int(input("Enter a fundamental frequency: ")) print(*weird_function(freq)) root = tk.Tk() canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH) canvas.pack() #background_image = tk.PhotoImage(file='') #background_label = tk.Label(root,image=background_image) #background_label.place(relwidth=1,relheight=1) frame = tk.Frame(root, bg='#80c1ff', bd=5) frame.place(relx=0.5, rely=0.3, relwidth=0.5, relheight=0.1, anchor='n') entry = tk.Entry(frame, font=40) entry.place(relwidth=0.65, relheight=1) button = tk.Button(frame, text="Get scale", font=40, command=lambda:weird_function(entry.get())) button.place(relx=0.7, relheight=1, relwidth=0.3) root.mainloop() ```how can I run this program successfully so first thing that come up is the canvas and when I enter the frequency at the entry it will return the function weird_function. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Struggling for the past hour to define function and call it back godlyredwall 2 184 Oct-29-2020, 02:45 PM Last Post: deanhystad How to define a function to create a resorted list? sparkt 6 415 Aug-08-2020, 04:10 PM Last Post: sparkt Cant define turtle color with function argument Wrightys99 2 373 Apr-22-2020, 01:43 PM Last Post: Wrightys99 How to define a function that calculates the BMI from dataframe DavidGG 2 1,115 May-30-2019, 03:35 PM Last Post: volcano63 How to define two functions run simultaneously within a function? Alberto 4 1,656 Feb-06-2018, 10:08 PM Last Post: Alberto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020