【问题标题】:TypeError: both arguments should be Rational instancesTypeError:两个参数都应该是 Rational 实例
【发布时间】:2022-06-11 00:05:07
【问题描述】:
# This is recipe conversion program
import fractions
from fractions import *

def getfile():
    filename = input("Enter file name: ")
    num_attempts = 1
    inputfile_limit = False
    while not inputfile_limit and num_attempts < 3:
        try:
            readFile = open(filename, 'r')
            inputfile_limit = True
        except IOError:
            print("File Open Error")
            num_attempts += 1
            filename = input("Enter file name: ")

    if num_attempts == 3:
        raise IOError('Exceed the number of file open attempt')
    return filename, readFile


def delete_digit(text):
    '''This function to delete all the digit of the original text'''
    k = 0
    if k < len(text) and text[k].isdigit() or text[k] == (' ', '/'):
        k += 1
    return text[k, len(text)]


def scanAsFractor(line):
    '''Scan all the input file including digits, fraction and return the fracitons object'''
    Scan_stop = False
    get_fraction = Fraction(0, 1)
    while not Scan_stop:
        k = 0
        while k < len(line) and line[k].isdigit():
            k += 1
        numerator = line[0:k]
        if k < len(line) and line[k] == '/':
            k += 1
            flash = k
            while k < len(line) and line[k].isdigit():
                k += 1
            denominator = line[flash:k]
        else:
            denominator = 1
        get_fraction = get_fraction + Fraction(numerator, denominator)
        if k == len(line):
            Scan_stop = True
        else:
            line = line[k:len(line)].strip()
            if not line[0].isdigit():
                Scan_stop = True
    return get_fraction


def convertline(line, factor):
    """If line begins with a digit, returns line with the value multiplied by factor. Otherwise, returns line unaterd"""
    global conv_line
    if line[0].isdigit():
        blank_char = ''
        frac_result = scanAsFractor(line) * factor
        if frac_result.getDenominator() == 1:
            frac_result = frac_result.getNumerator()
        conv_line = str(frac_result) + blank_char + delete_digit(line)
    else:
        conv_line = line
    return conv_line


# Main
# get file name and open file
file_name, input_filename = getfile()

# Get conversion factor
conv_factor = input("Enter the conversion factor: ")
conv_factor = scanAsFractor(conv_factor)

# Open output file name
output_file_name = "conv_" + file_name
output_file = open(output_file_name, 'w')

# Covert recipe
empty_str = ''
recipe_line = input_filename.readline()

while recipe_line != empty_str:
    recipe_line = convertline(recipe_line, conv_factor)
    output_file.write(recipe_line)
    recipe_line = input_filename.readline()


#close file
input_filename.close()
output_file.close()

这是复制输入文件内容的配方转换程序,格式如下: 巧克力曲奇食谱 9/2 杯通用面粉 2茶匙小苏打 2杯黄油,软化 3/2杯装红糖 1/2 白糖 1/2 白糖 2包速溶香草布丁混合物 4个鸡蛋 2茶匙香草精 4杯半甜巧克力片 2杯切碎的核桃

但是我收到了这个错误,你能帮我运行我的代码吗?


Enter file name: chocchipcookie.txt
Enter the conversion factor: 2
Traceback (most recent call last):
  File "/Users/mac/Downloads/CS/object-oriented programming/A Recipe Conversion Program/recipe conversion program.py", line 78, in <module>
    conv_factor = scanAsFractor(conv_factor)
  File "/Users/mac/Downloads/CS/object-oriented programming/A Recipe Conversion Program/recipe conversion program.py", line 48, in scanAsFractor
    get_fraction = get_fraction + Fraction(numerator, denominator)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/fractions.py", line 174, in __new__
    raise TypeError("both arguments should be "
TypeError: both arguments should be Rational instances

【问题讨论】:

标签: python


猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多