Skip to main content
GameDev.net gamedev.net
🔒 Locked

Kinda hard to explain this

Started by Calcious May 23, 2016 at 5:03 AM 9 replies 2.3k views
Original Post
Calcious
Calcious


subcores = ["AX43", "JOM10", "BALLS", "AYVOC", "PIZZA", "TITAN"]
cores = ["Fast", "Strong", "Balanced"]
Whole_Item = []
active = 1
add_item_active = 1
item_select = []
core_prompt = ("Select one of them, spell them exactly like it is spelled like." + "\n")
subcores_prompt = ("Select one of the core, spell them exactly like it is spelled like." + "\n")
add_item = ""
#####################





print("trash code below!\nThese are the cores available for you:")


for Cheese in cores:
	print("\t" + Cheese)
	
cheese_select = input(core_prompt)

if cheese_select in cores:
	print("Adding in " + cheese_select.title() + ".")
else:
	print("This isn't in the list, sorry.")
	active = 0
	
	
if active == 1:
	print("Now for the Sub Cores:")
	
	for Item in subcores:
		print("\t" + Item)
		
	item_holder = input(subcores_prompt)
	if item_holder in subcores:
		item_select.append(item_holder)
	else:
		print("This isn't an item, sorry.")
		active = 0
		
	if add_item_active == 1:
		add_item = input("Would you like to add  more items? If so, enter Yes. \n")
	
	while add_item == "Yes" or "yes":
		add_item_active = 0
		extra = input("Please select another sub core.\n")
		if extra in subcores:
			print(extra + " has been added!")
			item_select.append(extra)
		#checks again... if add_item is no or No, go to the if statement below. If Yes or yes, while loop still goes on
		add_item = input("Would you like to continue adding more items?.\n")		
if add_item == "No" or "no":
	print("This is your finalized core thingy:")
	item_select.append(cheese_select)
	Whole_Item = item_select[:]
	for whole in Whole_Item:	
		print("\t" + whole)

				
			
	

So I had to do a project for a python book and I made this. The last comment above should tie in with my current problem.

Even if I type in No or No, the while loop still goes on even though it should stop since it requires add_item to be "Yes" or "yes".

(also, do if statements only run once?)

Tanay Karnik
Tanay Karnik

The problem lies in these lines:


while add_item == "Yes" or "yes":

In the first line, your checking two conditions:

  • add_item == "Yes" checks whether add_item is equal to the string literal "Yes", if no your second condition is evaluated otherwise you enter the loop
  • "yes" Yeah, the second condition is simply "yes". Since, "yes" is a string literal, not equal to zero, the value will come out to be True whatever be the value of add_item.

You have to understand properly what the or operator does. The or operator simply evaluates two boolean values (one to its left, the other to its right). If either of them is True, then it returns True.

To accomplish what you were trying to accomplish you should do something like


while add_item == "Yes" or add_item == "yes":

The same problem exists in another line of your code, try to find it out and correct it.

Also, in your code, you are not checking the case in which the user inputs somethings other than yes and no.

do if statements only run once?

Every piece of code is evaluated every time the interpreter comes across it. If you place the if-statement in a loop then it will be evaluated as many times as the loop loops.

You need to understand the control flow better. Flow charts might help.

Don't hurry while learning, make sure you understand everything completely before moving ahead. If you don't understand anything properly, use various resources to understand the same part, this will clear a lot of misconceptions.

BeerNutts
BeerNutts

Also, this is wrong:


    item_holder = input(subcores_prompt)
    if item_holder in subcores:
        item_select.append(item_holder)
    else:
        print("This isn't an item, sorry.")
        active = 0
        
    if add_item_active == 1:

You're setting active to 0, you need to be setting add_item_active = 0

Good luck, and keep working at it!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
mrpeed
mrpeed

Just to mention, you can save some horizontal space and accomplish the same thing with a set and the in operator (good if you have lots of options):


while add_item in {"YES", "yes"}:

Instead of:


while add_item == "Yes" or add_item == "yes":
Calcious
Calcious

I think the python book that I'm using isn't really going in depth.

If you guys can recommend some books that has the right balance of complex and (intuition???), it would be great!

frob
frob

Does LearnPython.org cover it, or are you looking for something more?

Calcious
Calcious

LearnPython.org seems good but I don't know what some of the words are. I guess I can google those words.

Alberth
Alberth

I think the python book that I'm using isn't really going in depth.

If you guys can recommend some books that has the right balance of complex and (intuition???), it would be great!

"Right balance" is a personal thing. I don't think it is bad if you don't learn everything the first time.

Your primary aim is to be able to express what you want to do. The "or" with two "==" tests does that.

Once you can do that comfortably, you'll start to pick up new and smarter ways of doing things from other people suggesting alternative ways, or code or other books that you read. "in" is one such example, but Python has loads of them. It's an ever lasting process to improve your coding.

Calcious
Calcious

It feels like I'm learning a programming language, not learning programming itself. The book I'm reading teaches python (and is for absolute beginners). You would think that an absolute beginner would learn the concepts behind programming?

Alberth
Alberth

For a large part you are correct, and it's the expected order.

Just as a child first learns words before sentences, and at primary school you learn mostly only writing.

At a later age, you learn about higher order structures, such as paragraphs, sections, order between sections and chapters, consistency of style, order of arguments, completeness of points of view, and so on.

This is also a logical order, there is no point in explaining how to write a reasoning or argumentation, do proper layout (like double empty line when you switch subject), if you are not comfortable writing arbitrary sentences first.

Programming languages are also languages, and you see much the same steps there.

On the other hand, the number of words is very limited, so there isn't much to learn in terms of words. Programming starts relatively early.

Programming concepts technically start as soon as you write programs that are longer than 1 statement. Example


a = 3
b = a

is different from


b = a
a = 3

(for the more advanced readers: Yes, there is the exception being the case that 'a' was already 3)

This is actually programming already. Understanding how statements work, and seeing what effect a sequence of statements has. Seeing the exception is a skill that takes much longer to develop, since you have to reverse the question. (You have to answer "for what values a and b are both sequences the same", which is a much more difficult question than "what does this code do for "a=4 and b = 1".)

Many tutorial books stay quite close to a language, and this is what you expect tbh. just like a book named "Excel 2013" won't teach you use of spreadsheets in general, or its application in financial management in a company.

For this reason it is extremely important not to just read the book, but also write code, toy with it, try things. Fail, understand why, and fix. You can do that with exercises provided by a book, but you can also invent your own programs, or extend programs that you wrote as exercise. Learning by doing is by far the best way to learn programming (that holds for many other skills too).

More game-oriented exercises, higher/lower or hang-man games are very easy to write. You'd need a while loop, assignment and expressions (a = b + 1 things) , if statement, and for higher/lower random(), but we can explain that particular item. For hang-man, you also need strings, lists, and indexing (mylist[3]).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.