简体   繁体   English

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

[英]Can someone help me with my code? (python)

So I get this error message:所以我收到此错误消息:

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

I bet it will be obvious but, I'm new to python and need some help.我敢打赌这很明显,但是,我是 python 新手,需要一些帮助。

code:代码:

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?")

Any help will be greatly appreciated.任何帮助将不胜感激。 I got the m to work, but then I implemented o and it didn't work.我让 m 工作,但后来我实现了 o 并且它没有用。

This code is supposed to work on python 3.这段代码应该适用于 python 3。

If you are using python 2, then wherever you want your input to be of integer type, use如果您使用的是 python 2,那么无论您希望输入为整数类型,请使用

int(raw_input()) int(raw_input())

to accept the input.接受输入。 Whenever you want it to be a string, simply use每当您希望它是一个字符串时,只需使用

raw_input()原始输入()

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

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