-4

So, for my school project, that's due tomorrow, I was tasked with making a simple game from if statements. That is exactly what I tried doing, but whenever I run the game and try buying something else other than tomatoes, I still end up buying tomatoes. I don't know what I did wrong, so I am hoping someone can help me.

I'm really new to coding so I'm sorry if this is an annoying question or anything like that.

Here is the code:

money = 50           #Money that is in the players pocket
print("Welcome to our Shop!")
print("You have " + str(money) + " dollars in your wallet.") #starting balance
if input("Would you like to enter? Y/N ") == 'Y' or 'y': #go into the shop
    print("You entered the shop.")
    if input("You are at the vegetables isle. Do you want to buy tomatoes, ($5) potatoes ($7) or cucumbers? ($10) ") == 'tomatoes' or 'Tomatoes' or 'tomato' or 'Tomato' or 't' or 'T':    #choose to buy tomatoes
        sum = int(money) - 5
        print("You bought tomatoes. You have " + str(sum) + " dollars left.") #end balance if you choose tomatoes
    elif input("You are at the vegetables isle. Do you want to buy tomatoes, ($5) potatoes ($7) or cucumbers? ($10) ") == 'potatoes' or 'Potatoes' or 'potato' or 'Potato' or 'p' or 'P':   #choose to buy potatoes
            sum = int(money) - 7
            print("You bought potatoes. You have " + str(sum) + " dollars left.") #end balance if you choose potatoes
    elif input("You are at the vegetables isle. Do you want to buy tomatoes, ($5) potatoes ($7) or cucumbers? ($10) ") == 'cucumbers' or 'Cucumbers' or 'cucumber' or 'Cucumber' or 'c' or 'C': #choose to buy cucumbers
                sum = int(money) - 10
                print("You bought cucumbers. You have " + str(sum) + " dollars left.") #end balance if you choose cucumbers
    if input("Would you like to enter? Y/N ") != 'Y' or 'y': #don't go into the shop
        print("You turned around and walked away from the shop.\n")

Thanks. Hope you can help me.

  • 1
    Welcome to SO. Firstly, without looking at anything else, you need to correct the main if statement. ```input() == 'Y' or 'y'``` does not do what you want. Try it out with a two-line Python code to check how it responds to your inputs. You would need: ```input() == 'Y' or input() == 'y'```. Or, for this case, ```input().lower() == 'y'``` – fdireito Nov 14 '21 at 20:10
  • There are many logical mistakes in your code but can you explain what are you trying to do? shop until you have no money left? – ImSo3K Nov 14 '21 at 20:19
  • Thanks for the response! That did not change the response, it only seemed to simplify the code. Here is the output: You have 50 dollars in your wallet. Would you like to enter? Y/N y You entered the shop. You are at the vegetables isle. Do you want to buy tomatoes, ($5) potatoes ($7) or cucumbers? ($10) cucumbers You bought tomatoes. You have 45 dollars left. – hiyariyt Nov 14 '21 at 20:40
  • Here is the code: – hiyariyt Nov 14 '21 at 20:41
  • you should use `text = input()` before `if/else`, not inside `if input()` /else input()` – furas Nov 14 '21 at 22:01

2 Answers2

1

As already said, there are multiple issues with your code.
First, as already mentioned, input() == 'Y' or 'y' does not work. You need to use input().lower() == 'y', or input() in ['y','Y']. You should definately use this list method in your later checks, as it shortens your if clauses a lot.
Second, using buit in function names as a variable name is never a good idea, because you can't use the function later on.
Third, mind your indentation. You don't need to indent code more for every if statement, you can just indent it the same.
Fourth, you should not convert your money variable to int every time, as it is already int.
Fifth, you should save the result of your inputs into variables. Now, if the user types in patatoes in the first input, then there will be asked him another time what he wants to buy instead of the user being noticed that he just buyed patatoes. I would suggest the following:

money = 50           #Money that is in the players pocket
print("Welcome to our Shop!")
print("You have " + str(money) + " dollars in your wallet.") #starting balance
yn=input("Would you like to enter? Y/N ") 
if yn.lower() == 'y': #go into the shop
    print("You entered the shop.")
    tobuy=input("You are at the vegetables isle. Do you want to buy tomatoes, ($5) potatoes ($7) or cucumbers? ($10) ")
    if tobuy.lower() in ["tomatoes","tomato","t"]: #choose to buy tomatoes
        balance = money - 5
        print("You bought tomatoes. You have " + str(balance) + " dollars left.") #end balance if you choose tomatoes
    elif tobuy.lower() in ["patatoes","patato","p"]:   #choose to buy potatoes
        balance = money - 7
        print("You bought potatoes. You have " + str(balance) + " dollars left.") #end balance if you choose potatoes
    elif tobuy.lower()in ["cumcumbers","cumcumber","c"]: #choose to buy cucumbers
        balance = money - 10
        print("You bought cucumbers. You have " + str(balance) + " dollars left.") #end balance if you choose cucumbers
else: #don't go into the shop
    print("You turned around and walked away from the shop.\n")

In the following example, the user is asked what he wants to buy every time again, such that he can buy multiple things. Do also notice that you should change the original money variable instead of creating a new one, such that his money decreases every time he buys something.

money = 50           #Money that is in the players pocket
print("Welcome to our Shop!")
print("You have " + str(money) + " dollars in your wallet.") #starting balance
yn=input("Would you like to enter? Y/N ") 
if yn.lower() == 'y': #go into the shop
    print("You entered the shop.")
    print("You are at the vegetables isle.")
    if input("Do you want to buy tomatoes($7)?").lower()=='y': #choose to buy tomatoes
        money = money - 5
        print("You bought tomatoes. You have " + str(money) + " dollars left.") #end balance if you choose tomatoes
    if input("Do you want to buy potatoes ($7)").lower() == 'y':   #choose to buy potatoes
        money = money - 7
        print("You bought potatoes. You have " + str(money) + " dollars left.") #end balance if you choose potatoes
    if input("Do you want to buy cucumbers? ($10) ").lower() == 'y': #choose to buy cucumbers
        money = money - 10
        print("You bought cucumbers. You have " + str(money) + " dollars left.") #end balance if you choose cucumbers
else: #don't go into the shop
    print("You turned around and walked away from the shop.\n")
The_spider
  • 1,202
  • 1
  • 8
  • 18
-1
from dataclasses import dataclass
from typing import List


@dataclass
class Product:
    name: str
    plural: str
    price: int
    key: chr

    def __str__(self):
        return f'{self.plural}, (${self.price})'


@dataclass
class Customer:
    money: int
    products: List[Product]

    def buy(self, p: Product):
        self.money -= p.price
        self.products.append(p)
        print(f'You bought {p.name}. You have {self.money} dollars left.')

    def enter(self, s):
        print("You entered the shop.")
        s.vegetables_isle(self)


@dataclass
class Shop:
    products: List[Product]

    def vegetables_isle(self, c: Customer):
        print("You are at the vegetables isle.")
        print("Do you want to buy", end=' ')
        for p in self.products[:-1]: print(p, end = ' ')
        print(f'or {self.products[-1]}')
        answer = input('choice: ')
        for p in self.products:
            if answer.lower() in [p.name, p.plural, p.key]:
                return c.buy(p)


if __name__ == '__main__':
    shop = Shop(products = [
        Product(name='tomato', plural='tomatoes', price=5, key='t'),
        Product(name='potato', plural='potatoes', price=7, key='p'),
        Product(name='cucumber', plural='cucumbers', price=10, key='c'),
    ])

    customer = Customer(50, products=[])

    choice = input("Would you like to enter? Y/N ")

    if choice.lower()  == 'y':
        customer.enter(shop)
    else:
        print("You turned around and walked away from the shop.\n")
mama
  • 2,046
  • 1
  • 7
  • 24
  • 1
    Please be sure to review [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). – Jeremy Caney Nov 15 '21 at 00:49