简体   繁体   English

在满足条件之前,我该如何重复“ if”语句?

[英]How do I make a repeating 'if' statement until a condition is met?

I'm trying to make a python program that runs the Euclidian algorithm. 我正在尝试制作一个运行Euclidian算法的python程序。 This is my current code: 这是我当前的代码:

a = float(input())
b = float(input())
aplha = float(a/b)
omega = float(b/a)

import math
if a > b:
    print(str(a) + " = " + str(b) + " * " + str(math.floor(aplha)) + " + " + str(a%b)) 
elif b > a:
    print(str(b) + " = " + str(a) + " * " + str(math.floor(omega)) + " + " + str(b%a)) 
else:
    print("Both numbers have the same value. The greatest common denominator is 1.")

How do I make it so that the if and elif keeps repeating themselves until a%b = b%a = 0? 我如何使if和elif不断重复直到a%b = b%a = 0?

Here is one way to implement the Euclidean algorithm. 这是实现欧几里得算法的一种方法。

import math

a = float(input())
b = float(input())

# The greatest common denominator of a and b
def gcd(a,b):
    while (b != 0):
       t = b
       b = a % b
       a = t 
    return a

if (a > b):
    print(f'{a} = {b} * {math.floor(a/b)} + {a%b}')
else:
    print(f'{b} = {a} * {math.floor(b/a)} + {b%a}')


print(f'The greatest common denominator of {a} and {b} is {gcd(a,b)}')

if a==b it is not necessarily true that the GCD is one. 如果a==b ,则GCD不一定为1。 Consider a = 150 and b = 150 as a counter example. 考虑a = 150b = 150作为反例。 The greatest common denominator of a and b is 150. gcd(a,b) = 150 . ab的最大公分母为gcd(a,b) = 150

Also a note on print(f'string{var}') . 还要注意print(f'string{var}') Print f-string is new in Python 3 and really helpful for printing the value of variables. Print f-string是Python 3中的新增功能,对于打印变量值确实很有帮助。 The way it works is 它的工作方式是

>>> var = 5
>>> print(f'The value of var is {var}')
"The value of var is 5"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让计算机检查列表并添加直到满足特定条件 - how do i make the computer check a list and add until a certain condition is met 如何让我的循环检查 if 语句直到满足条件/基本上直到按钮出现在屏幕上? - How to make my loop check the if statement until the condition is met / basically until the button appears on screen? 重复 Dialogflow 意图直到满足条件 - Repeating Dialogflow Intent Until Condition is Met 如果满足 if 语句的条件,如何停止 while 语句中的错误消息? - How do i stop error message from the while statement if the condition for the if statement is met? 我想要一个 if 语句在满足条件时将矩形添加到列表中,但它会不断添加它们直到条件为假 - I want an if statement to add a rectangle to a list, when a condition is met, but it keeps adding them until the condition is false 如何正确地创建一个条件,一旦条件满足就会退出 while 循环? - How do I properly make a condition that will exit a while loop once a condition has been met? 如何让程序不断重复,直到我输入停止它的特定数据? - How do I make a program keep repeating until I input a specific data that stops it? 如何使程序循环直到满足条件? - How can I make it so that the program loops until the conditions are met? 如何等待协程直到满足条件? - How to await a coroutine until a condition is met? 如何迭代直到满足python for循环中的条件 - How to iterate until a condition is met in python for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM