私はPythonのかなり新しい初心者です。最大数と最小数、および数の間の範囲を見つけるためのサブルーチンを含むコードを作成しました。しかし、最大数、最小数、さらには範囲を見つけようとしているときに、テストデータの答えが間違っています。テストデータx = 12、y = 6、z = 2は、最大数がyであることを示しています。
注:変数は、選択肢1が出力するものなどのoutput1です。
x = input("Enter x:")
y = input("Enter y:")
z = input("Enter z:")
if x>y and x>z :
output1 = 'x is the largest'
large = x
elif (y>x and y>z):
output1 = 'y is the largest'
large = y
elif (z>x and z>y):
output1 = 'z is the largest'
large = z
else :
output1 ='all numbers are equal'
large = 0
if x<y and x<z :
output2 = 'x is the smallest'
small = x
elif (y<x and y<z):
output2 = 'y is the smallest'
small = y
elif (z<x and z<y):
output2 = 'z is the smallest'
small = z
else :
output2 = 'all numbers are equal'
small = 0
output3 = large-small
outputq = "Bye"
print("[1] Find the highest variable.")
print("[2] Find the lowest variable.")
print("[3] Find the range between the highest and the lowest variables.")
print("[q] Quit.")
while outputq == ('Bye'):
choice = input("Enter choice number:")
if choice == '1' :print (output1)
elif choice == '2' :print (output2)
elif choice == '3' :print (output3)
elif choice == 'q' :
print (outputq)
outputq="end"
input ()
if x>y and x>z :
input()は文字列を返すため、コードでは数値ではなく文字列を比較します。それをintに変換します:
x = int(input("Enter x:"))
y = int(input("Enter y:"))
z = int(input("Enter z:"))
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加