笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册
查看: 4896|回复: 1

[已经解决] 编程中遇到的小问题请高手指教

[复制链接]

新手上路

Rank: 1

积分
6
发表于 2022-3-7 11:40:23 | 显示全部楼层 |阅读模式
  1. import easygui as g
  2. class Centigrade:
  3.       
  4.       # 初始化对象
  5.       def __init__(self,value = 0):
  6.             self.value = float(value)

  7.       # 查看摄氏温度属性
  8.       def __get__(self,instance,value):
  9.             return self.value
  10.       
  11.       # 设置摄氏温度属性
  12.       def __set__(self,instance,value):
  13.             self.value = float(value)

  14. class Fahrenheit:

  15.       # 查看华氏温度属性
  16.       def __get__(self,instance,value):
  17.             return instance.cel * 1.8 + 32
  18.       
  19.       # 设置华氏温度属性
  20.       def __set__(self,instance,value):
  21.             instance.cel = (float(value) - 32) / 1.8

  22. class Temp:
  23.       # 赋值实例对象
  24.       cel = Centigrade()
  25.       fah = Fahrenheit()

  26. def cel_to_fah():
  27.       g.msgbox('欢迎使用摄氏温度转华氏温度')
  28.       t = Temp()
  29.       t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
  30.       g.msgbox('华氏温度为:' + str(t.fah))

  31. def fah_to_cel():
  32.       g.msgbox('欢迎使用华氏温度转摄氏温度')
  33.       t = Temp()
  34.       t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
  35.       g.msgbox('摄氏温度为:' + str(t.cel))
  36.       

  37. g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
  38. if True:
  39.       cel_to_fah()
  40. else:
  41.       fah_to_cel()
复制代码
学习了Property原理后写了段代码运行后ccbox无论按那个按钮都运行摄氏转华氏,不知为什么?请问如何改正!
何建钰已获得悬赏 10 C币

最佳答案

if True: cel_to_fah() 只能进True
回复

使用道具 举报

新手上路

Rank: 1

积分
9
发表于 2022-3-7 11:41:19 | 显示全部楼层
if True:
      cel_to_fah()
只能进True

  1. import easygui as g
  2. class Centigrade:
  3.       
  4.       # 初始化对象
  5.       def __init__(self,value = 0):
  6.             self.value = float(value)

  7.       # 查看摄氏温度属性
  8.       def __get__(self,instance,value):
  9.             return self.value
  10.       
  11.       # 设置摄氏温度属性
  12.       def __set__(self,instance,value):
  13.             self.value = float(value)

  14. class Fahrenheit:

  15.       # 查看华氏温度属性
  16.       def __get__(self,instance,value):
  17.             return instance.cel * 1.8 + 32
  18.       
  19.       # 设置华氏温度属性
  20.       def __set__(self,instance,value):
  21.             instance.cel = (float(value) - 32) / 1.8

  22. class Temp:
  23.       # 赋值实例对象
  24.       cel = Centigrade()
  25.       fah = Fahrenheit()

  26. def cel_to_fah():
  27.       g.msgbox('欢迎使用摄氏温度转华氏温度')
  28.       t = Temp()
  29.       t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
  30.       g.msgbox('华氏温度为:' + str(t.fah))

  31. def fah_to_cel():
  32.       g.msgbox('欢迎使用华氏温度转摄氏温度')
  33.       t = Temp()
  34.       t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
  35.       g.msgbox('摄氏温度为:' + str(t.cel))
  36.       

  37. ans=g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
  38. if ans:
  39.       cel_to_fah()
  40. else:
  41.       fah_to_cel()
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 09:52 , Processed in 0.023207 second(s), 24 queries .

© 2001-2020

快速回复 返回顶部 返回列表