繁体   English   中英

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

[英]Can someone help me with my code? (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 并且它没有用。

这段代码应该适用于 python 3。

如果您使用的是 python 2,那么无论您希望输入为整数类型,请使用

int(raw_input())

接受输入。 每当您希望它是一个字符串时,只需使用

原始输入()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM