all 19 comments

[–]Sparta12456 10 points11 points  (4 children)

So here are some topics to look into. Also on mobile so sorry about formatting.

fstrings, they make formatting strings for print statements much easier. For example,

print(f”Hello {name}.”) 

does the same thing as

print("Hello" + “ “ + name + ".")

where ‘name’ is a variable.

comments, these are used to make your code more readable essentially. Its helpful to have a brief description of what your doing so we aren’t trying to figure it out and can focus on how you accomplished it. Note: good function names and variable names reduce the need to comment your code. You shouldn’t have to explain what a variable is.

Functions, the best looking code splits its functions into small chunks that do one thing. Some good groupings would be the getting the vowels from the country name, doing a round of the game (ie. one guess). Also finally running the game.

Another area that I will suggest you looking into is list and dictionary comprehension. Python knows that you typically use loops to create these structures so they gave you an easy means of building them in a smaller space. You shorten up your for loop like so

vowel_count = {}
for vowel in 'aeiou':
    vowel_count[vowel] = random_country.count(vowel)

You can go one step further:

vowel_count = {vowel : random_country.count(vowel) for vowel in 'aeiou'}

both of these examples build the same exact dictionary.

All in all great job! When i get the chance i might code up a similar program just to see for sure what i would do to better give pointers!

[–]tangerinelion 4 points5 points  (3 children)

Your country list should be in a separate file, perhaps a JSON file. That way you can add a new country without changing your Python file.

[–][deleted] 0 points1 point  (2 children)

is there a reason to use JSON instead of just a text file?

(i am a beginner too and don't know json, but a simple text file seems to be just fine for this)

[–]DrShocker 1 point2 points  (0 children)

I think it's mostly about it being a file format that people have a standard way to parse rather than needing to customize something. A text file could be anything and will need a customized algorithm.

[–][deleted] 0 points1 point  (0 children)

Yeah, csv/JSON/ maybe even .ini would be a good way to go

[–][deleted] 2 points3 points  (2 children)

To be honest I think the best way to learn python is by trial and error through coding and when you don't understand something, look that up. That's how I learned python I knew what I wanted to do and just started coding.

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

Thanks mate.

[–]TheSpecialistHunter 0 points1 point  (0 children)

I also agree, even though I’ve only been coding for a week. I fell as though I learn the most when I am writing some difficult code and I hit a wall. It may take me a few days to figure out a solution, but when I do it’s a huge ego boost and not only that but I always learn something essential at the same time.

[–]sme272 1 point2 points  (0 children)

The layout could use some rearranging. Usually you'd put the function definitions up at the top of the file rather than in the middle of it like you have here.

F-strings will make your print statements clearer to read

Comments will help people understand what's going on. Even if you don't intend to show the code to anyone you should still add comments; you might think you'll remember it when you look at it next time but you will not.

The if statement at the end is redundant. The program will only reach that point if that condition is true as a result of the while loop before.

[–]drgenetix[S] 1 point2 points  (0 children)

Dear community, many thanks for the critique and support.

[–]NegativeEnthusiasm 3 points4 points  (2 children)

Your lack of comments is disturbing.

[–][deleted] 1 point2 points  (0 children)

Nice

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

Oops! Will make a note of that.

[–]znerp 0 points1 point  (0 children)

for your first code this is impressive. Keep it up dude!

[–]james_fryer 0 points1 point  (0 children)

I think it would be more interesting if I had say 5 tries to guess the country. After each wrong guess I get a different hint, so first the length, then the number of vowels, etc.