Pythonで何かをするのはこれが初めてです...または他のプログラミング言語はまったくありません。
以下は、単純な電卓での私の最初の試みです。
print ("Hello")
def calc():
x = int(input("Input first integer: "))
y = int(input("Input second integer: "))
type = str.lower(input("(A)dd, (S)ubstract, (M)ultiply, (D)ivide \n"))
if type != "a" and type != "s" and type != "m" and type != "d":
print ("Sorry, the command you entered is not valid.")
calc()
else:
if type =="a":
print ("The result is '" + str(x+y) + "'")
elif type == "s":
print ("The result is '" + str(x-y) + "'")
elif type =="m":
print ("The result is '" + str(x*y) + "'")
elif type == "d":
print ("The result is '" + str(float(x)/float(y)) + "'")
if int(input("Enter 1 if you would like to perform another calculation? \n")) == 1:
calc()
else:
exit()
calc()
それはうまくいくようです。次に、指数と剰余をミックスに追加しようとすると、構文エラーが発生します。
print ("Hello")
def calc():
x = int(input("Input first integer: "))
y = int(input("Input second integer: "))
type = str.lower(input("(A)dd, (S)ubstract, (M)ultiply, (D)ivide, (E)xponent, (R)emainder\n"))
if type != "a" and type != "s" and type != "m" and type != "d" and type != "e" and type != "r":
print ("Sorry, the command you entered is not valid.")
calc()
else:
if type == "a":
print ("The result is '" + str(x+y) + "'")
elif type == "s":
print ("The result is '" + str(x-y) + "'")
elif type == "m":
print ("The result is '" + str(x*y) + "'")
elif type == "d":
print ("The result is '" + str(float(x)/float(y)) + "'")
elif type == "e":
print ("The result is '" + str(x**y + "'")
elif type == "r":
print ("The result is '" + str(x%y) + "'")
if int(input("Enter 1 if you would like to perform another calculation? \n")) == 1:
calc()
else:
exit()
calc()
私が間違ってやっている簡単なことはありますか?また、終了するには「Q」を追加する必要があります...プロセスをより簡単に説明するものを誰かに紹介してもらえますか?