Is there a way to shorten the codes I used in that game(to save time)? I'm new to "functions" and "modules" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there a way to shorten the codes I used in that game(to save time)? I'm new to "functions" and "modules"

https://code.sololearn.com/cs9nkk3iwXe1/?ref=app This one is a game of attributes, I used Repl it.com(an online IDE), at first I used a "for loop" but it didn't work as I had two variables which either didn't do their job or gave me errors, I had to use a "while loop" instead, I mainly used if conditionals, I was happy to be able to do it but it took me some long time.... https://code.sololearn.com/cCl8M5fwU5wg/?ref=app An example for the "for loop" that was supposed to run the game for 30 loops with the two variables x and y, would it have been better to use a for loop?

26th Jan 2020, 11:55 AM
Ali Abdelhady
Ali Abdelhady - avatar
17 Answers
+ 7
It is time for the next steps. you seem to be save in the algorithm and actually about 10 statements in each language are enough to code almost everything. If i was u, I would spend a week for further concepts that u already mentioned in ur post. Without that u won't become a good programmer. After you can answer your question yourself. U might think the land of functions and classes is dark and unfriendly but it is as little as the monster under your bed when you were young. Actually classes and functions have potential for becoming good friends. But u must qualify for it. They are worth it
26th Jan 2020, 12:45 PM
Oma Falk
Oma Falk - avatar
+ 4
What is the purpose of (z = x + y) in the first line ? About the second code, it can be much shorter, but only if you remove the useless prints like print('Water rocks') and print('OHH !') The way you wrote so many if/else is completly bad, and not practical.
26th Jan 2020, 12:00 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Ali Abdelhady yes, there is no need for it all. Why did you store x and y in it ?
26th Jan 2020, 12:07 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Ali Abdelhady saying so means that you do not know how for loops work. I suggest you go back and revise the course starting from loops.
26th Jan 2020, 12:13 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Ali Abdelhady for loops are in chapter 2 of the Python course. Looking at your progress in Python, you are almost in the middle of the course, so the loops should be completly understood. Which is why I asked you to go back and revise them, because you don't seem to have understood it well.
26th Jan 2020, 12:21 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Atleast you can simplify all the 20 print statements of: print('something attack VS something attack') Using format string: print(f"{X} attack VS {Y} water attack") Where X and Y represent variables assigned to the attack titles.
26th Jan 2020, 12:03 PM
Seb TheS
Seb TheS - avatar
+ 2
Ali Abdelhady fstring (format string) is a string that treats all characters written between { } as a code and inserts the result to the string. f"A{6*8}B" ---> "A48B" a = 10 f"C{a}D" ---> "C10D" b = "F" f"{b}" ---> "F"
26th Jan 2020, 12:20 PM
Seb TheS
Seb TheS - avatar
+ 1
Aymane Boukrouh Because I used it in the "for z in range" code I store x and y in it, you mean there is no need for it?
26th Jan 2020, 12:04 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Aymane Boukrouh You are right I guess it all relates that it just feels weird for me that I only mention "z"or "i" in the "for loop" as a variable with no value to be used
26th Jan 2020, 12:11 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Seb TheS what is that format string thing? Could you clarify more?
26th Jan 2020, 12:16 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Aymane Boukrouh well, I haven't finished the course yet
26th Jan 2020, 12:18 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Aymane Boukrouh I will, thank you
26th Jan 2020, 12:23 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Seb TheS Thanks!
26th Jan 2020, 12:31 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Good
27th Jan 2020, 8:03 AM
Khun Aung Htun Lay
Khun Aung Htun Lay - avatar
0
So macj
27th Jan 2020, 10:12 PM
Albert Askandarov
Albert Askandarov - avatar
0
U8uuuhhhoopl
28th Jan 2020, 11:39 AM
arun kumar
0
I wrote a code for the modified game with some new stuff yesterday, gotta thank you all for your help :)
31st Jan 2020, 5:04 PM
Ali Abdelhady
Ali Abdelhady - avatar