有人能帮我写代码吗?(Python)

2024-10-01 17:33:28 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我得到这个错误信息:

Traceback (most recent call last):
  File "C:/Users/Ethan/Documents/Coding/test2.py", line 3, in <module>
    buy = input("Marijuana plants cost 200$ ea, opium seeds cost 300$")
  File "<string>", line 1, in <module>
NameError: name 'm' is not defined

我敢打赌这是显而易见的,但是,我是python新手,需要一些帮助。你知道吗

代码:

balance = 2500
FinalPrice = 0
buy = input("Marijuana plants cost 200$ ea, opium seeds cost 300$")     
while (buy != "o") and (buy != "m"):
    buy = input("That's not on the market! type 'm' if you want to buy Marijuana and 'o' if you want opium!")
if buy =="o":
    o = input("How many opium seeds? Press c to cancel")
    if o=="0":
        o = input("invalid number, input again")
    elif o =="c":
       input("You cancelled the trade. Type in a command to do something else")
    oprice = (o*300)
    print(oprice)
    FinalPrice-=FinalPrice
    FinalPrice+=oprice
    obuy = input("This is the final price, press b to buy or c to cancel")
    if obuy =="c":
        input("You cancelled the trade. Type in a command to do something else")
    elif obuy =="b":
        if oprice > balance:
            print("Not enough money! Sell more drugs to earn more money.")
        elif oprice < balance:
            print("you bought", o , "Opium seeds for", oprice , "$")
    input("What do you want to do next?")    
elif buy =="m":
    m = input("How many Marijuana plants? Press c to cancel")
    if m=="0":
        m = input("invalid number, input again")
    elif m =="c":
        input("You cancelled the trade. Type in a command to do something else")
    mprice = (m*200)
    print(mprice)
    FinalPrice-=FinalPrice
    FinalPrice+=mprice
    mbuy = input("This is the final price, press b to buy or c to cancel")
    if mbuy =="c":
        input("You cancelled the trade. Type in a command to do something else")
    elif mbuy =="b":
        if mprice > balance:
            print("Not enough money! Sell more drugs to earn more money.")
        elif mprice < balance:
            print("you bought", m , "Marijuana plants for", mprice , "$")
input("What do you want to do next?")

任何帮助都将不胜感激。 我得到了m的工作,但后来我实现了o,它没有工作。你知道吗


Tags: thetoinyouinputifbuydo
1条回答
网友
1楼 · 发布于 2024-10-01 17:33:28

这段代码应该适用于python3。你知道吗

如果您使用的是python2,那么无论您希望输入是整数类型,都可以使用

int(原始输入())

接受输入。 无论何时,只要使用

原始输入()

相关问题 更多 >

    热门问题