Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将OOP与此基本计算脚本结合使用?_Python_Oop - Fatal编程技术网

Python 如何将OOP与此基本计算脚本结合使用?

Python 如何将OOP与此基本计算脚本结合使用?,python,oop,Python,Oop,我正试图写一个简单的计算器来计算我的电池将以多快的速度从100%消耗到0%,方法是从开始的时间和百分比中减去结束的时间和百分比 我能够制作一个快速而肮脏的脚本来完成这项任务,但我正在学习OOP,我希望获得一些关于如何改进代码和实现此任务的OOP的反馈。我添加了一些详细的print语句,以便在运行时更容易记住所有内容,我打算将其打包成一个简单的bash脚本,或者实现tkinter for GUI 任何想法或建议都会有帮助!我的主要环境是Python3,但我为那些使用python2运行的人添加了错误

我正试图写一个简单的计算器来计算我的电池将以多快的速度从100%消耗到0%,方法是从开始的时间和百分比中减去结束的时间和百分比

我能够制作一个快速而肮脏的脚本来完成这项任务,但我正在学习OOP,我希望获得一些关于如何改进代码和实现此任务的OOP的反馈。我添加了一些详细的print语句,以便在运行时更容易记住所有内容,我打算将其打包成一个简单的bash脚本,或者实现tkinter for GUI

任何想法或建议都会有帮助!我的主要环境是Python3,但我为那些使用python2运行的人添加了错误处理,因为它不喜欢时间数中前面的0

from datetime import timedelta

# Getting inputs for time and percentage

try:
    start_time = str(input('What is the starting time in 24 hour time? Format: HHMM >> '))
    start_time_hr = start_time[:2]
    start_time_min = start_time[2:]
except SyntaxError:
    print('Try entering the time again without the preceding 0. \n')
    start_time = str(input('What is the starting time in 24 hour time? Format: HHMM >> '))
    start_time_hr = start_time[0]
    start_time_min = start_time[1:]

start_perc = int(input('What is the starting battery percentage? >> '))

try:
    end_time = str(input('What is the finish time in 24 hour time? Format: HHMM >> '))
    end_time_hr = end_time[:2]
    end_time_min = end_time[2:]
except SyntaxError:
    print('Try entering the time again without the preceding 0. \n')
    end_time = str(input('What is the finish time in 24 hour time? Format: HHMM >> '))
    end_time_hr = end_time[0]
    end_time_min = end_time[1:]

end_perc = int(input('What is the ending battery percentage? >> '))

# Turning numbers into times for calculation

start = timedelta(hours=int(start_time_hr), minutes=int(start_time_min))
end = timedelta(hours=int(end_time_hr), minutes=int(end_time_min))

# Calculating and printing the results

perc_difference = float(-(end_perc - start_perc))
time_difference = end - start
time_difference_minute = time_difference.total_seconds() / 60
discharge = (100.0 * time_difference_minute / perc_difference) / 60

print()
print()
print('*****')
print('Percentage Difference = ' + str(perc_difference))
print('Minutes Passed = ' + str(time_difference.total_seconds() / 60))
print('100% to 0% in ~' + str(round(discharge, 2)) + ' hours.')
print('*****')
print()
print()
print()

我认为这个程序不会从使用OOP中受益

也就是说;我确实有一些关于风格和缺乏防御性编程的注释

第一,;您不需要用
str
包装
input
调用,因为它默认返回字符串

第二;您已经用
int
包装了几个
input
调用,这很好,但是这些调用需要包装在
try
块中,因为任何无法转换为整数的输入都会引发
ValueError

第三;您不应该以这种方式处理语法错误。如果出现语法错误,请修复代码,不要试图用绷带将其包裹起来

第四;您不需要编写那么多
print
语句

整个打印块可以重写如下:

print('*****\nPercentage Difference = ' + str(perc_difference) + 'Minutes Passed = ' + str(time_difference.total_seconds() / 60)) + "\n100% to 0% in ~' + str(round(discharge, 2)) + ' hours.\n' +
*****')
或者,在我看来;更具可读性的变体:


改进工作代码的请求位于codereview.stackexchange.com。也就是说,并不是每个任务都能从OOP中获益。此外,您在这个脚本中所做的任何事情都不会产生一个
SyntaxError
,您可以在运行时在Python3中捕获它,如果您使用的是Python2,那么您应该使用
raw\u input
,而不是
input
msg = f"*****\nPercentage Difference = {perc_difference}\nMinutes Passed = {time_difference.total_seconds() / 60}\n100% to 0% in ~{round(discharge,2)} hours.\n"

print( msg )