执行程序的时候出现这个问题,但是我没有加
if name == "main"这个语句啊(加了结果也是这个错误)查询了一下,也定义了__init__方法,看了一下,在init中调用了同一个类下面的方法,然后在下面的方法中又调用了方法之一,但是不知道怎么改
希望赐教
```python
import math
import time
import pymysql
from collections import deque
from timeit import Timer
import pickle
class LessonOne:
pi = math.pi
def __init__(self):
global choiceNum,result
while True:
print("###############")
print("1. 球的运算")
print("2. 周薪运算")
print("3. 弹跳高度运算")
print("4. pi的运算")
print("5. 信贷计划")
print("6. 结束")
print("7.查看用户的输入记录")
print("###############")
try:
choiceNum = int(input("请选择一个功能:"))
if choiceNum == 1:
self.ballOperation()
elif choiceNum == 2:
self.weekMoneyOperation()
elif choiceNum == 3:
self.bounceOperation()
elif choiceNum == 4:
self.piOperation()
elif choiceNum == 5:
self.payOperation()
elif choiceNum == 6:
print("欢迎您下次使用")
elif choiceNum ==7:
self.inputDate()
break
else:
print("输入错误,请输入数据1-6作出选择")
except ValueError:
print("请输入数据1-6作出选择")
def ballOperation(self):
global temp,result
while True:
try:
temp = input("请输入球体的半径(输入#退出):")
r = float(temp)
d = 2 * r
p = 2 * self.pi * r
a = 4 * self.pi * r * r
v = 4/3 * self.pi * r * r * r
result = d
print("球的直径:%0.2f" % d)
print("球的周长:%0.2f" % p)
print("球的表面积:%0.2f" % a)
print("球的体积:%0.2f" % v)
except ValueError:
if temp == "#":
print("欢迎您下次使用!")
break
else:
print("您输入的球的半径有误,请重新输入 ")
self.inputDate()
def weekMoneyOperation(self):
global temp,result
while True:
try:
temp = input("请输入时薪:(输入#退出)")
hourMoney = float(temp)
if hourMoney > 5000:
print("您输入的工资太大了,是不是有误,请判断")
continue
except ValueError:
if temp == "#":
break
else:
print("时薪必须是整数或小数")
continue
try:
normalHours = float(input("请输入一周的工作时间(小时):"))
except ValueError:
print("工作时间必须是整数或小数")
continue
try:
moreHours = float(input("请输入一周的加班时间(小时)"))
allMoney = hourMoney * normalHours + hourMoney*1.5*moreHours
result = allMoney
print("一周工资为:%0.2f" % allMoney)
except ValueError:
print("加班时间必须是整数或小数")
continue
self.inputDate()
def bounceOperation(self):
global temp,result
while True:
try:
temp = input("请输入球的初始高度:(输入#退出)")
height = float(temp)
ratio = float(input("请输入球的弹跳系数,是0-1之间的小数:"))
bounceNums = int(input("请输入允许球弹跳的次数"))
distance = 0
for a in range(bounceNums):
distance = distance + height + height * ratio
height = height * ratio
print("球运行的距离是:%0.2f" % distance)
result = distance
except ValueError:
if temp == "#":
break
else:
print("输入有误,请重新输入")
self.inputDate()
def piOperation(self):
global temp,result
while True:
try:
temp = input("请输入迭代次数:(输入#退出)")
count = int(temp)
exp = 0
j = 1
pi = 0
for i in range(count):
if i % 2 == 0:
exp = exp + 1/j
else:
exp = exp - 1/j
j = j+2
pi = 4 * exp
print(str(pi))
result = pi
except ValueError:
if temp == "#":
break
else:
print("请输入正确的迭代次数!")
self.inputDate()
def payOperation(self):
global temp,result
YEAR_RATE = 0.12
MONTH_RATE = YEAR_RATE/12
FIRST_PAY_RATE= 0.1
while True:
try:
temp = input("请输入购买价格:(输入#退出)")
buyPrice = float(temp)
restPay = buyPrice - (buyPrice * FIRST_PAY_RATE)
monthPay = 0.05 * restPay
print("月数 当前所欠的余额 当月所欠的利息 当月所欠的本金 当月所需付款金额 付款之后所欠的金额")
month = 1
while restPay>0:
monthInterest = restPay * MONTH_RATE
if restPay > monthPay:
benjin = restPay - monthPay
restPay = restPay - monthPay + monthInterest
else:
benjin = 0
monthPay = monthInterest + restPay
restPay = 0
print("%4d %8.2f %16.2f %16.2f %16.2f %16.2f" % (month,restPay,monthInterest,benjin,monthPay,restPay))
month = month + 1
result = month
except ValueError:
if temp == "#":
break
else:
print("您输入的价格有误")
self.inputDate()
def inputDate(self):
global temp
#if __name__ == '__main__':
t1 = Timer("ballOperation()","from __main__ import ballOperation")
t2 = Timer("weekMoneyOperation()","from __main__ import weekMoneyOperation")
t3 = Timer("bounceOperation()","from __main__ import bounceOperation")
t4 = Timer("piOperation()","from __main__ import piOperation")
t5 = Timer("payOperation()","from __main__ import payOperation")
if choiceNum == 1:
operateTime = t1.timeit(1000000)
elif choiceNum == 2:
operateTime = t2.timeit(1000000)
elif choiceNum == 3:
operateTime = t3.timeit(1000000)
elif choiceNum == 4:
operateTime = t4.timeit(1000000)
elif choiceNum == 5:
operateTime = t5.timeit(1000000)
else:
operateTime = 0
db = pymysql.connect(host="localhost",
user="root",password="root",port=3306,
db='python',
charset="utf8")#连接数据库,添加cursor游标
cursor = db.cursor()#创建MySQL请求的对象
start = time.time()
sql = "select * from test"
name = ""
if choiceNum == 1:
name =="球的运算"
elif choiceNum == 2:
name == "周薪运算"
elif choiceNum == 3:
name == "弹跳高度运算"
elif choiceNum == 4:
name == "pi的运算"
elif choiceNum == 5:
name == "信贷计划"
elif choiceNum == 6:
name == "结束"
elif choiceNum == 7:
name == "查看用户输入记录"
else:
name == "#"
while True:
# if temp == "#":
# name == "none"
try:
cursor.execute(sql)
cursor.execute(choiceNum,name,temp,result,start,operateTime)
cursor.fetall()#查询作用
db.commit()
except Exception as err:
db.rollback()
db.close()
#if __name__ == '__main__':
#main()
a = LessonOne()
```