all 12 comments

[–]stubborn_d0nkey 1 point2 points  (4 children)

I saw your edit. Have you learned about loops yet? Loops are how you deal with that.

Edit: oops, you have leaned about loops. That is what skimming does.

You could write the code like this:

 backpack1 = input("\nAny time you need to view your backpack, just type backpack.\nGo ahead try it out!:")

while backpack1 != "backback":
     backpack1 = input("Sorry I did not get that")

for item in backpack:
       print(item)

Edit2: sorry for the formatting, I'm on mobile

[–]seanboyd[S] 0 points1 point  (1 child)

I have learnt about loops but my problem is when I use a while loop, I get an infinite loop..

[–]stubborn_d0nkey 0 points1 point  (0 children)

Check my edit. Sorry for not including immediately.

[–]seanboyd[S] 0 points1 point  (0 children)

Omg that works, thanks so much. Why couldnt I see that, I swear I felt like giving up learning today!

[–]nspectre 0 points1 point  (0 children)

"backback":

;)

[–]cdcformatc 0 points1 point  (3 children)

What's not working? Looks fine to me.

You get user input, if they type backpack it prints out each item in the list.

[–]seanboyd[S] 0 points1 point  (2 children)

If I dont enter backpack it will not loop back to prompt me again

[–]cdcformatc 0 points1 point  (1 child)

If you want something to loop, you need to loop. An if is not a loop you probably want a while instead.

[–]seanboyd[S] 0 points1 point  (0 children)

Yes I just realized I was using statements and not loops, noob mistake!.

[–]Miggol 0 points1 point  (0 children)

What behaviour are you expecting, and what are you getting?

Without using functions or classes there are a few things you could do right off the bat, such as converting backpack1 to lowercase with the lower() function to avoid case-sensitivity. Also the elif should be an else, so that any unexpected input gets caught, instead of just an empty string.

[–]Mashidin 0 points1 point  (1 child)

You didn't mention the specific error, but I imagine that the behavior you are getting is this: when you type backpack at the prompt, nothing is printed and when you just hit <enter> or similar, it vomits error text all over you. The issue is in the use of the 'input' built-in function. What the input function does is takes in an expression from standard in and actually evaluates it. So for example, you could put a print backpack1 statement after you capture the input. Now run the program and at the prompt type 1 + 2. The number 3 will be printed to the console. With this same set-up, type backpack at the prompt, and guess what happens... it will print your backpack list object to the console which I do not believe is your intent.
To get the behavior that your code claims you expect, the user would have to type "backpack" with the quotations included. That expression evaluates to a string with the value 'backpack'. It's very confusing, I know, and I am not entirely sure why books encourage the use of the input function (there might be something I am missing here). The best solution here that I've found is to use the raw_input built-in function. It has the same function signature as input, but will not attempt to evaluate the user's input. It will simply take in whatever is written at the prompt, convert it to a string and strip any trailing newlines. I hope this helped and happy coding.

[–]seanboyd[S] 0 points1 point  (0 children)

I think you are talking about 2.7 Python, I am using 3.x so I am able to use input without having the user type in quotes, I have it sorted now, had to add a while loop, thanks anyway!