Question

I am having a hard time writing an IDLE code (Python 3.7.2) and I need help....

I am having a hard time writing an IDLE code (Python 3.7.2) and I need help. Here are the parameters.

1. Write a program that will generate a random backpack. This program will have a menu that allows the

user to continuously select between generating a new backpack, printing the current backpack, adding a

given number of an item, removing a given number of an item or quitting. Every backpack will have a

variable number of the following 7 items: magic item, clothes item, food item, armor item, fire creation

item, weapon item, other item. You can use a two-dimensional list if you wish, but I suggest using two

lists. One list for the names of the items and a second list that holds the number of each item. Note:

removing an item should never result in a negative number of an item, it should just take it to zero.

Assume this is a super amazing backpack of holding so there is no maximum on any item

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#class backpack
class backpack:
    #variables
    items=["magic","clothes","food","armor","fire_creation","weapon","other"]
    value=[0,0,0,0,0,0,0]
#main method definition
def main():
    #infinite loop
    while True:
        #showing options to users
        print "1. Create new backpack\n2. Print current backpack\n3. Add/Remove items to backpack\n4. Exit\n"
        #taking user input
        ch=input("Select an Option: ");
        if ch==1:
            #creating object
            b1=backpack();
            continue
        elif ch==2:
            ##printing the details of the backpack
            print b1.items[0],b1.value[0]
            print b1.items[1],b1.value[1]
            print b1.items[2],b1.value[2]
            print b1.items[3],b1.value[3]
            print b1.items[4],b1.value[4]
            print b1.items[5],b1.value[5]
            print b1.items[6],b1.value[6]
            continue
        elif ch==3:
            #adding or removing items
            print "1. Add items\n2. Remove items\n3. Exit\n"
            inp=input("Select an option: ")
            if inp==1:
                #adding items
                b1.value[0]=b1.value[0]+input("Magic: ")
                b1.value[1]=b1.value[1]+input("Clothes: ")
                b1.value[2]=b1.value[2]+input("Food: ")
                b1.value[3]=b1.value[3]+input("Armor: ")
                b1.value[4]=b1.value[4]+input("Fire: ")
                b1.value[5]=b1.value[5]+input("Weapon: ")
                b1.value[6]=b1.value[6]+input("Other: ")
            elif inp==2:
                #removing items
                m=input("Magic: ")
                c=input("Clothes: ")
                f=input("Food: ")
                a=input("Armor: ")
                fr=input("Fire: ")
                w=input("Weapon: ")
                o=input("Other: ")
                if m>b1.value[0] or c>b1.value[1] or f>b1.value[2] or a>b1.value[3] or fr>b1.value[4] or w>b1.value[5] or o>b1.value[6]:
                    #showing error message
                    print "Selected item values cannot be removed. Try again..."
                else:
                    b1.value[0]=b1.value[0]-m;
                    b1.value[1]=b1.value[1]-c;
                    b1.value[2]=b1.value[2]-f;
                    b1.value[3]=b1.value[3]-a;
                    b1.value[4]=b1.value[4]-fr;
                    b1.value[5]=b1.value[5]-w;
                    b1.value[6]=b1.value[6]-o;
            else:
                break
            continue
        else:
            print "The option is invalid. Exiting..."
            break
#calling main function
if __name__ == '__main__':
    main()

Output

1. Create new backpack
2. Print current backpack
3. Add/Remove items to backpack
4. Exit

Select an Option: 1
1. Create new backpack
2. Print current backpack
3. Add/Remove items to backpack
4. Exit

Select an Option: 2
magic 0
clothes 0
food 0
armor 0
fire_creation 0
weapon 0
other 0
1. Create new backpack
2. Print current backpack
3. Add/Remove items to backpack
4. Exit

Select an Option: 3
1. Add items
2. Remove items
3. Exit

Select an option: 1
Magic: 1
Clothes: 2
Food: 3
Armor: 4
Fire: 5
Weapon: 6
Other: 7
1. Create new backpack
2. Print current backpack
3. Add/Remove items to backpack
4. Exit

Select an Option: 2
magic 1
clothes 2
food 3
armor 4
fire_creation 5
weapon 6
other 7

Add a comment
Know the answer?
Add Answer to:
I am having a hard time writing an IDLE code (Python 3.7.2) and I need help....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Python Problem, just need some guidance with the description, pseudocode and run time. I believe this...

    Python Problem, just need some guidance with the description, pseudocode and run time. I believe this is an 0-1 knapsack problem? Shopping Spree: (20 points) Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example,...

  • C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally...

    C++: Need help debugging my code I am writing this and using some other examples as reference code while rewriting it with my own understanding of the material but am having trouble making it finally compile. Below is a picture of the error messages. //main.cpp //Semester Project //Created by J---on 5/6/2019 #include <iostream> #include <fstream> #include <string> #include <sstream> #include <bits/stdc++.h> using namespace std; void instructions(); //displays program details and instructions void openFile(); void takeInput(int*); void switchBoard(int*); struct price {...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • I need help writing python code with following instructions. You will write a program that reads...

    I need help writing python code with following instructions. You will write a program that reads a data file. The data file contains ticket IDs and ticket prices. Your job is to read these tickets (and prices). find minimum, maximum and average ticket prices and output a report file. The report file should look exactly (or better than) the one attached (output.txt). Input: A31 149.99 B31 49.99 A41 179.99 F31 169.99 A35 179.99 A44 169.99 open "input.txt" file using open()...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT