返回功能值以确定要打印的内容

时间:2018-06-14 23:59:20

标签: python

我将使用参数" n"创建一个函数。我想要一个函数返回" n"如果为0或0,则用户输入将被视为小于或等于0.如果为1,则用户输入将被视为大于0.

这是我到目前为止所做的,而且我不确定如何纠正我的错误。

from tkinter import *
import tkinter as tk
window = Tk()
lbl1 = Label(window, text="# of rows: 3").grid(column=0, row=0)
lbl2 = Label(window, text="# of cols: 2").grid(column=0, row=1)
window.geometry('250x250')
def clickedc():     #create matrix click event
    row =  (3)      #In final version these will be the numbers I loop over when 
    col =  (2)      #making the entries in question.
    lblA = Label(window, text="Input matrix:").grid(column=(int(col/2)), row=3)
    entry_list = [] #This is what I want to be a list I can .get() from later
    r = 1
    for r in range(int(row)):
        entry_list.append([])
        c = 1       
        for c in range(int(col)):
            entry_list[-1].append(tk.Entry(window, width=10).grid(column=c, row=4 + r))

    def clickeds():         #Solving click event
        r = 1
        matrix_A = []       #The matrix I want to put the values into, for reference
        for r in range(int(row)):
            c = 1
            for c in range(int(col)):
                matrix_A[r - 1:c - 1] = entry_list.get("1.0",'end-1c') #Causes Error   
            #Here I'll: output a new matrix as a grid of labels.ex: #1  1
    btn_s = Button(window, text="Solve", command=clickeds)          #1  0
    btn_s.grid(column=int(col) + 1, row=3)                          #0  1

btn_c = Button(window, text="Create", command=clickedc)
btn_c.grid(column=3, row=1)

window.mainloop()

2 个答案:

答案 0 :(得分:2)

你的问题是你已经覆盖了整数的定义。以下脚本工作正常。此外,n*0是不必要的,因为它总是评估为0.

def main():
    n = int(input("Enter a value: "))
    fb=integer(n)
    if fb == 0:
        print("That value is less than or equal to 0")
    else:
        print("That value is greater than 0")
def integer(n):
    if n <= 0:
        return 0
    elif n > 0:
        return 1

main()

答案 1 :(得分:0)

请尝试以下代码,它要短得多:

def main():
    d = {0:'That value is less than or equal to 0',1:"That value is greater than 0"}
    n = int(input("Enter a value: "))
    fb=integer(n)
    print(d[fb])
def integer(n):
    return 0 if n <= 0 else 1

main()

我只是简单地创建一个字典