+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?

1/26/2020 11:55:51 AM

Ali Abdelhady

21 Answers

New Answer

+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

+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.

+4

Ali Abdelhady yes, there is no need for it all. Why did you store x and y in it ?

+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.

+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.

+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.

+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"

+2

Actually, shortening of codes do take much time compared to just writing. The only thing in code shortening is line reduction (not time reduction), you actually need to think well before shortening a code and that perhaps takes time but not spaces. some tips to shorten your code, always use functions or functional programming ternary operator for loop e.t.c

+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?

+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

+1

Seb TheS what is that format string thing? Could you clarify more?

+1

Aymane Boukrouh well, I haven't finished the course yet

+1

Aymane Boukrouh I will, thank you

+1

Seb TheS Thanks!

+1

0

0

U8uuuhhhoopl

0

I wrote a code for the modified game with some new stuff yesterday, gotta thank you all for your help :)