gpt4 book ai didi

python - 我的第一个程序

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:03 25 4
gpt4 key购买 nike

我在 Code Academy 上了很多课,但我感到很沮丧,因为一些编译器会拒绝返回相同结果的答案。因此,我决定暂时脱离并使用我学到的工具(计算器)创建自己的程序。它无法正常运行。当它在菜单中时,if/elif/else 语句不起作用。该程序似乎忽略了输入。所以,这是我的第一个程序代码...

import math
user_choice=(">>")
def add():
print "What two numbers would you like to add?"
a= int(raw_input(user_choice))
b= int(raw_input(user_choice))
c= a + b
print c
def sub():
print "What two numbers would you like to subtract?"
a=int(raw_input(user_choice))
b=int(raw_input(user_choice))
c=a-b
print c
def mult():
print "What two numbers would you like to multiply?"
a=int(raw_input(user_choice))
b=int(raw_input(user_choice))
c= a*b
print c
def div():
print "What two numbers would you like to divide?"
a=int(raw_input(user_choice))
b=int(raw_input(user_choice))
c=a/b
print c
def exp():
print "What number would you like to power?"
a=int(raw_input(user_choice))
print "By what number would you like it to be powered to?"
b=int(raw_input(user_choice))
c= math.pow(a,b)
print c
def square():
print "What number would you like to square root?"
a=int(raw_input(user_choice))
b=math.sqrt(a)
print b

print "+---------------------------+"
print "| Welcome to my basic |"
print "| calculator! |"
print "| |"
print "|What would you like to do? |"
print "| |"
print "|1: Addition |"
print "|2: Subtraction |"
print "|3: Multiplication |"
print "|4: Division |"
print "|5: Exponents |"
print "|6: Square Root |"
print "|7: Quit |"
print "| |"
print "+---------------------------+"

if int(raw_input(user_choice)) == "1":
add()

elif int(raw_input(user_choice)) == "2":
sub()

elif int(raw_input(user_choice)) == "3":
mult()

elif int(raw_input(user_choice)) == "4":
div()

elif int(raw_input(user_choice)) == "5":
exp()

elif int(raw_input(user_choice)) == "6":
square()

elif int(raw_input(user_choice)) == "7":
exit()

else:
print "Sorry, I didn't understand your entry.Try entering a value 1-7"

还没有“if error”代码,但我只是坚持让它工作。所有的功能都有效。只是无法使选项起作用。

最佳答案

int(rawinput()) 将返回一个整数,它不会是== 到像"1 这样的字符串“。从它们中删除 int(),它应该可以工作。

关于python - 我的第一个程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com