日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎么显示分数_在Python中使用分数

發布時間:2025/3/19 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么显示分数_在Python中使用分数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我在這里使用類來輸入一個分數(當給定分子和分母時),以及將兩個分數相加和相乘。出于某種原因,導入的分數模塊只對程序的一部分正確工作;gcd方法工作,但是分數方法(當給定兩個數字時,放入分數格式)不工作,而是拋出一個NameError(特別是“未定義全局名稱‘分數’)。

我做錯什么了?我對Python還比較陌生,對于如何在出現更多異常的情況下使代碼更緊湊的建議,我將非常感激。

這是我的代碼:import fractions

class FractionClass:

# Initialize starting numerator and denominator.

n = 0

d = 0

def __init__(self, numerator, denominator):

self.n = numerator

self.d = denominator

# Function that adds fractions, whichs throws NameError if gcd() doesn't work!

def add_fractions(self, other):

try:

sum_numerator = self.n + other.n

sum_denominator = fractions.gcd(self.d, other.d)

return(sum_numerator, sum_denominator)

except NameError:

print("Methods aren't defined.")

# Function that multiplies fractions, whichs throws NameError if gcd() doesn't work!

def multiply_fractions(self, other):

try:

product_numerator = self.n * other.n

product_denominator = self.d * other.d

return(product_numerator, product_denominator)

except NameError:

print("Methods aren't defined.")

# Enter input function.

def user_input():

try:

print("Enter a numerator and denominator:")

n, d = [int(x) for x in input().split()]

print("Enter a numerator and denominator:")

n2, d2 = [int(x) for x in input().split()]

# Check used to debug for denominators that aren't the minimum of 1 (0 can't be divided!)

check = 1 / d

check = 1 / d2

# print(check)

# Exception for d = 0.

except ZeroDivisionError:

print("\n You didn't enter the minimum denominator.")

print("Set denominator to minimum default.")

d = 1

# Exception for not entering a space in between numbers.

except UnboundLocalError:

print("You didn't enter your numbers in properly! Try again.")

# Exception for not entering all required.

except NameError:

print("\n You didn't enter two numbers.")

# Exception for user input both or one of them, not being integers.

except TypeError:

print("\n You didn't enter all valid numbers.")

# General exception case.

except:

print("Something went wrong!")

fract = FractionClass(n,d)

another_fraction = FractionClass(n2, d2)

total_sum = fract.add_fractions(another_fraction)

# Unpacks total sum tuple.

# Puts in fraction format.

sum_numerator, sum_denominator = total_sum

add_output = fractions.Fraction(sum_numerator, sum_denominator)

total_product = fract.multiply_fractions(another_fraction)

# Unpacks product sum tuple.

# Puts in fraction format.

product_numerator, product_denominator = total_product

multiply_output = fractions.Fraction(product_numerator, product_denominator)

print(add_output, multiply_output)

總結

以上是生活随笔為你收集整理的python怎么显示分数_在Python中使用分数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。