gpt4 book ai didi

How do I convert text value from a textbox to an integer?(如何将文本框中的文本值转换为整数?)

转载 作者:bug小助手 更新时间:2023-10-24 18:59:34 28 4
gpt4 key购买 nike



My assignment is to create a program to take 3 sides of a triangle and determine whether it is equilateral, isosceles, scalene, or not a triangle. There are 3 textboxes for the user to enter the lengths of the sides of the triangle, and a button to trigger its function. I keep getting errors when it goes to convert the user-inputted text to an integer or float. I'll copy my code below.

我的任务是创建一个程序来获取一个三角形的三条边,并确定它是否是等边、等腰、等边或不是三角形。有3个文本框供用户输入三角形的边长,还有一个按钮用于触发其功能。在将用户输入的文本转换为整型或浮点型文本时,我一直收到错误。我将在下面复制我的代码。


I expected it to convert the user-inputted text from the textboxes to integers in order to be able to compare them mathematically to determine the type of triangle.

我预计它会将用户输入的文本从文本框转换为整数,以便能够通过数学比较来确定三角形的类型。


Please let me know if I need to share more detail, this is the first time I've asked questions on here, though I've been reading for a couple of years.

如果我需要分享更多细节,请让我知道,这是我第一次在这里提问,尽管我已经读了几年了。


import flet as ft

def main(page: ft.Page):

############ function ##################
def is_triangle(side1, side2, side3):
side1 = float(side1.value);
side2 = float(side2.value);
side3 = float(side3.value);
if side1 + side2 > side3 or side2 + side3 > side1 or side1 + side3 > side2:
get_type(side1, side2, side3);
return(get_type(side1, side2, side3))
else:
return("Not a triangle");

def get_type(side1, side2, side3):
if side1 == side2 and side2 == side3:
return("The Triangle is equilateral")
else:
side1!= side2 and side2!= side3 and side1!= side3;
return("The Triangle is scalene");

output_textField.value = f"{is_triangle()}"
page.update()

############# page setup ################
page.title = "Triangle Type Identifier"
page.window_width = 700
page.window_height = 700
page.theme_mode = "light"
page.window_center()
page.vertical_alignment = ft.MainAxisAlignment.SPACE_EVENLY
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

########### page content ###############
title_text = ft.Text("Enter the length of each side of a triangle", color = "BLUE", size = 30)

########### input ###############
input1 = ft.Text("Enter side 1 of a triangle", color = "BLACK", size = 25)
side1 = ft.TextField(width = 100, height = 100, color = "BLACK", text_size = 20, text_align = "center")

input2 = ft.Text("Enter side 2 of a triangle", color = "BLACK", size = 25)
side2 = ft.TextField(width = 100, height = 100, color = "BLACK", text_size = 20, text_align = "center")

input3 = ft.Text("Enter side 3 of a triangle", color = "BLACK", size = 25)
side3 = ft.TextField(width = 100, height = 100, color = "BLACK", text_size = 20, text_align = "center")


############ button ###############
get_type_button = ft.ElevatedButton(
content = ft.ResponsiveRow(
[ft.Text("Get Type of Triangle", size = 20, text_align = "center")]
),
width = 300, height = 100, bgcolor = "BLUE", color = "WHITE",
on_click = is_triangle(side1, side2, side3)
)

########### output ###############
output_text = ft.Text("Triangle Type", color = "BLACK", size = 25)
output_textField = ft.TextField(
width = 100, height = 100, color = "BLACK", text_size = 20, text_align = "center"
)

page.add(
title_text,
input1,
side1,
input2,
side2,
input3,
side3,
get_type_button,
output_text,
output_textField
)
ft.app(target = main)

This was as far as I got. I keep getting the following error.
I tried using "a, b, c" within function "is_triangle" and that got it to where
it'd open the window at least, but then it would say
'TypeError: int() argument must be a string, a bytes-like object or a real
number, not 'TextField'

我只能走到这一步了。我一直收到以下错误。我尝试在函数“is_triangle”中使用“a,b,c”,这样至少可以打开窗口,但它会说‘TypeError:int()参数必须是字符串、类似字节的对象或实数,而不是’Textfield‘。“




Unhandled error processing page session : Traceback (most recent call last):
File "C:\Users\agcqu\anaconda3\lib\site-packages\flet_runtime\app.py", line 357, in on_session_created
session_handler(page)
File "c:\users\agcqu\cs120\3.4 lab_3_quigley_anna.py", line 55, in main
on_click = is_triangle(side1, side2, side3)
File "c:\users\agcqu\cs120\3.4 lab_3_quigley_anna.py", line 7, in is_triangle
side1 = float(side1.value);
ValueError: could not convert string to float:
"""

未处理的错误处理页面会话:回溯(最近一次调用):文件“C:\Users\agcqu\anaconda3\lib\site-packages\flet_runtime\app.py”,第357行,位于ON_SESSION_CREATED SESSION_HANDLER(PAGE)文件“c:\USERS\agcquu\cs120\3.4Lab_3_quigley_anna.py”,第55行,in Main on_Click=is_三角形(side 1,side 2,side 3)文件“c:\USERS\agcquu\cs120\3.4Lab_3_quigley_anna.py”,第7行,In is_triangleside 1=浮动(side 1.value);ValueError:无法将字符串转换为浮点型:“


更多回答
优秀答案推荐

I've never used Flet before but from what I can see you are passing in side1, side2 and side3 into the is_triangle function as if they are strings, but they are not. Those variables are being assigned to the output of the ft.TextField() constructor, meaning they are instances of the TextField class. Going by the documentation of that class, you are meant to use
the Textbox.value property to refer to the textbox's text value.

我以前从未使用过Flet,但从我可以看到的是,您正在将side 1、side 2和side 3传递到is_trangle函数中,就像它们是字符串一样,但它们不是。这些变量被分配给ft.TextField()构造函数的输出,这意味着它们是TextField类的实例。按照该类的文档,您应该使用Textbox.Value属性来引用TextBox的文本值。


更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com