DEV Community

Cover image for Project in Python
Eddie
Eddie

Posted on

Project in Python

I can’t tell you how many videos and books and articles I’ve consumed on Python. At first I was driving myself even crazier because I was also doing the same for Swift code. But I stopped Swift and have been focusing on Python. Unsure if I’m making progress. Not really, no. 
I’ll follow along with a video, do the code on my mac or iPad, and all is good. I’ll tweak it here and there and watch what happens. But generally it’s all not sinking in. I tried to take some killer Notion apps, like I see on YouTube. But I cannot access Notion the 8 hours I’m in an office, and the internet is sometimes too slow to access it on my MBP or iPad when I’m out of the Operations Center and its lame computer/network. 
Earlier I had Googled for some beginner projects. I liked the one for choose your own adventure. I did a simple choice last week where it asked about pineapple and pizza. This time I started from scratch and started to move down a path. 
One thing that is strange to me is that when I put in this:

name = input("What is your name?").strip()

print(name, ", before you is an old gate. It appears to be quite old, with vines snarled around the bars so thickly that you cannot see through to the other side.") 
Enter fullscreen mode Exit fullscreen mode


I get a space after the name variable when I have it in a string. 



Alt Text

That little space after my name and before the comma drives me nuts. But I’ll hold off on that detail. For now I’m pressing on. Let’s add some loops. I saw in one video how you can add the .lower() and it will take a YES or Yes and convert it all to a lowercase ‘yes’. Otherwise, I’d have to write some code checking for this in the loop. Neat trick. 



Alt Text

I’ll admit that I goofed when I made this at first. I was testing it with each step of the way, and before I wrote the else at the end, it was an infinite loop and kept printing the last sentence a bazillion times and crashed Safari. HA! I added the else and it fixed it. 
I wanted a way to keep track of progress, and right off the top of my head I named it block and gave it a value of 1. I used a simple += 1 with it and figured that I could do some while loops for various values of the block. Because this was a local variable, I had to use the global keyword. Not that I’m smart here, but I didn’t know how else to increment the variable up one as a result of a choice, but also have that variable defined outside of the function. This seems to work just fine. 



Top comments (6)

Collapse
 
flodevelops profile image
FloDevelops

I don't think you need any global declaration in your code.
And quick tip, print("a", "b") prints "a b", two elements each made of string, separated by a space. While print("a" + "b") prints "ab", one element made of two concatenated strings (+).

I love learning Python too ! 😁

Collapse
 
eddiecoyote profile image
Eddie

Much thanks! This was super helpful.

Collapse
 
eddiecoyote profile image
Eddie • Edited

Okay, I was wrong. I pulled out my computer, and it doesn't do anything like I thought. I keep getting declare errors between global and local variables, trying to define a function, and all that jazz. Wish I could keep working on this... but I need to get some sleep before work. It is a military deployment, after all.

This is so fun.

image of repl

Collapse
 
eddiecoyote profile image
Eddie

WOO HOO! I figured it out.

I tried creating a global variable before the functions, thinking that the functions would add to the global variable by 1. Didn't work. I added the global variable in each function, but because I had declared a global variable block = int(1) at the very beginning, it was messing it up. So I took that code out and it worked like a charm. It kept repeating my basic_move() until 4 times, and then told me that I won. HUZZAH!

a picture of my repl

Collapse
 
spiritupbro profile image
spiritupbro

love it still learning python cool bro

Collapse
 
eddiecoyote profile image
Eddie

Can’t sleep. Was thinking about this code. Opened this up and immediately saw an error. The second block += isn’t set to global. Therefore it creates another block variable, only this one is local.