Examples of code and personal projects

You can exit with sys.exit(exit_status) in python. Import the sys module to use it (which you didn’t do if I’m reading it correctly. Put import sys somewhere on the top of the file.).
enterinput.lower() will return enterinput in lowercase. str.upper is also a thing.
If one types in BACKGROUND you will still get the message used for unexpected input. That’s because the user didn’t write HELP, which means that the else after the help check fires. Use elif for the checks between the first if and the else so that once one check fires, the others don’t. Also, the check for unexpected input should be at the bottom of that if-elif chain.
e.g:

if enterinput == "QUIT":
    #blabla

elif enterinput == "BACKGROUND":
    #blabla

elif enterinput == "HELP":
    #blabla
        
elif enterinput == "ENTER":
    #blabla

else:
    #unexpected input

The user is also very unlikely to write sentences like «I get a Syntax Error when trying to run this» without differences. The in keyword can be used to search for elements in an array (such as strings or lists), if you still want to do it the way you are doing it.

1 Like

The help section are all placeholders as, unlike most of the updated code, there is nothing printed to the user. Once I’ve finished at least one country then I’ll revise the more confusing parts of the code. Thanks for the help!

It the product that the massive, failure, flowchart produced.

I think that this discussion fits better in this thread, to show off small coding learning projects.

2 Likes

Don’t often check development help, thought it was help for making mods for Python. Thanks for merging, would’ve never known this thread existed.

I’ll change the title to make it more general.

UPDATE: Changed.

1 Like

I made a non functioning Hangman. It makes a loop because it isn’t finished. Will be finished (hopefully) on Monday.
Filebin of the .py file (lasts 1 week):
https://filebin.net/nsyk2ttk3zx9fjyp

Source Code

import random

Game = True
while Game == True:

word = [“Array”, “Steam”, “Random”, “Import”, “Empires”, “Of”, “The”, “Undergrowth”, “Spore”, “Myrmecologist”, “Deoxyribonucleic”, “Plague”, “Pulsar”, “Ant”, “Thrive”, “Evolution”, “Ecosystem”, “Genetic”, “Microbe”, “Multicellular”, “Slug”, “Disco”, “Beetle”, “Uprising”, “Paradox”, “Star”, “Emperor”, “Espionage”, “Nemesis”, “Behaviour”, “Interactive”, “Dead”, “By”, “Daylight”, “Trapper”, “Wreith”, “Hag”, “Huntress”, “Myers”, “Shape”, “Nurse”, “Blight”, “Twins”, “Legion”, “Ghost”, “Face”, “Nightmare”, “Spirit”, “Executioner”, “Executioner”, “Pyramid”, “Head”, “Silent”, “Hill”, “Fallout”, “Elder”, “Scrolls”, “Ancestors”, “Humankind”, “Odyssey”, “Human”, “Revolutionary”, “Games”, “Studios”, “Aware”, “Awakening”, “Space”, “Ascended”, “Stage”, “Electronic”, “Arts”, “Hangman”, “Syntax”, “Error”, “Dungeon”, “Community”, “Biology”, “Xenophobia”, “Bacteriophage”, “Prion”, “Virus”, “Bacteria”, “Parasite”, “Agent”, “Gene”, “Disease”, “Microscopic”, “Transmission”, “Symptom”, “Infection”, “Infectivity”, “Lethality”, “Severity”, “Vector”, “Antibody”, “Antibiotics”, “Vaccine”, “Pandemic”, “Epidemic”]
#These are the words that’ll be used
lives = 10
#How many guesses you have
guessedletter = []
#Letters that the user has inputted
wordguess = []
#Shows player how long the word is
playerguess = “”
ChosenWord = random.choice(word).lower()
#The word
joinedWord = “”
for i in ChosenWord:
wordguess.append("?")
#Main
while (lives != 0 and “?” in wordguess):
joinedWord = “”.join(wordguess)
print(joinedWord)
print(“Please input a letter”)
letterinput = input().lower
if letterinput == wordguess:
print(“You got a correct letter, well done.”)
print(wordguess)
letterinput.append(playerguess)

for x in range(len(ChosenWord)):
    if playerguess ==  ChosenWord[x]:
      wordguess[x] = playerguess

if playerguess not in ChosenWord:
lives = lives - 1

print(“Do you want to play again? Y or N”)
responce = input()
if responce == “N”:
Game = False
else:
Game = True

The only difference is I fixed a Spelling mistake in the source code but not the file.

You should have used a code block as it looks like the formatting was entirely destroyed, which makes the python code invalid (thanks python people who decided that indentation is significant!).

import time and stuff

You can make code blocks like this:

```python
your code goes
here
```

edit: whoops, fixed valid to invalid

1 Like

I probably asked this before but am I the only one who finds low-level languages (C, C++) more intuitive than high-level/OOP languages (C#, Java)?

IDK I think most people just find the first language they code in to be the most intuitive.

For example I started coding in C# so now everything that’s very different from C# looks unintuitive to me. Like an explicit difference between pointers and variables for example. Apparently some people think it’s annoying that C# doesn’t tell you whether something is a pointer or a variable but for me it just sorta makes sense.
To this day I cannot pass a variable to a function in C++ without spending 20 minutes figuring out whether it should be a pointer or not. All I know about pointers is that C# just knows whether I meant to use a pointer.

So I guess all of our intuitions are just kinda weird in their own way.

I’ve only used Python so its the easiest I find to code in. Unless I’m tired, then its a nightmare.

If you make big systems in C, you’ll want to learn encapsulation strategies similar to OOP. Low level languages are only simple when you make simple programs. When you make much more complex software, you actually get more done with a more complex language as it supports building large systems better.

Surely if you practice enough in another language, you become proficient in it? I can’t really comment on this as I started writing C++, so for me moving to another language is basically noticing that most other languages only allow smart pointers to objects, so I just need to accept that I’m no longer in control of determining how a function receives the parameter objects. I really miss const references in C#. In C# you need to make intermediate interface types with read only properties, if you want to prevent a specific area of code from changing some object’s state.

1 Like

Here’s an example of code in C#. It creates an array containing the modern latin alphabet and digits.

using System.IO;
using System;

class Program
{
    static void Main(string[] args)
    {
        char[] array = new char[75]; /*This size is necessary because
        the for loop also counts the characters that are skipped*/
        
        for(int i = 0, j = 48; j <= 122; i++, j++)
        {
            if(j > 57 && j < 65 || j > 90 && j < 97)
            {
                continue; //skips values at [58, 64] and [91, 96]
            }
            
            array[i] = (char) j;
            Console.WriteLine(array[i]); //Prints all standard ASCII characters
        }
    }
}

Posting it here as a remainder for myself too.

sees hhyyrylainen’s reaction

@hhyyrylainen, what’s wrong?

1 Like

It’s actually really hard to follow the logic in there: two loop variables and skipping some ranges in the other, makes it very hard to follow.
I think you could use just one loop variable and calculate the corresponding characters for the array index.
And on top of that you also print each value in the loop, so storing them in the array isn’t even required.

More complicated program.

Eh

using System.IO;
using System;

class Program
{
    static void Main(string[] args)
    {
        char[] digits = new char[10];
        char[] latinAlphabet = new char[26];
        char[] latinSmallAlphabet = new char[26];
        int combinedSize = latinAlphabet.Length + latinSmallAlphabet.Length;
        
        for(int i = 0; i <= digits.Length - 1; i++)
        {
            int j = i + 48; //calculates ASCII digits' position
            digits[i] = (char) j; //converts the position into its character
        }
        
        for(int i = 0; i <= latinAlphabet.Length - 1; i++)
        {
            int j = i + 65; //calculates ASCII capital characters' position
            latinAlphabet[i] = (char) j; //converts it
        }
        
        for(int i = 0; i <= latinSmallAlphabet.Length - 1; i++)
        {
            int j = i + 97; //calculates ASCII small letters' position
            latinSmallAlphabet[i] = (char) j; //converts it
        }
        
        /*Combines both arrays containing the latin alphabet*/
        Array.Resize(ref latinAlphabet, combinedSize);
        latinSmallAlphabet.CopyTo(latinAlphabet, combinedSize/2);
        Array.Clear(latinSmallAlphabet, 0, latinSmallAlphabet.Length);
        
        foreach(char i in latinAlphabet)
        {
            Console.Write(i);
        }
        
        Console.WriteLine();
        
        foreach(char i in digits)
        {
            Console.Write(i);
        }
    }
}

It’s because I just wanted to test out the program. I also wished to use this code for another program where I needed to create an array containing the latin alphabet, but it was too long and boring. Not very readable too.

You could use multiple loops that act on the same array, but operate on different indices to use just two loops to get all the characters. Anyway, it’s a bit silly to try to really think too hard about this problem, as in real code you wouldn’t do this kind of character set building. I’ve never needed to do anything like this in real code, not just a toy example.

2 Likes

With C# game programming, which game engine do you think is the best to begin with for newbies?

I’m obviously below that level, lol.

Which game engine is the best for beginners (in C#)?
  • Godot
  • Unity

0 voters

I voted for Godot purely because I don’t want to see beginners trapped with unity and Godot is starting to have a big enough community that I don’t feel bad for recommending it to beginners.

1 Like

How’s Godot doing bugs-wise these days? One of the main things that put me off using godot before was how often you’d seem to run into issues with Thrive that you couldn’t solve because they were Godot things.

I only have experience with Unity so I have bias, but I haven’t used it in years so take my vote with a truck of salt.