If you have any questions, comments or issues with this project please post them here!
10 posts were split to a new topic: ValueError: All arrays must be of the same length when using pandas.DataFrame
New here and first question re the Day25 lesson. I’m familiar with subroutines and passing/returning variables. But I do admit I’m struggling a bit with how Python does “sub” things.
First, the example uses the same variable “area” both inside and outside the subroutine. This seems bad coding practice and a recipe for disaster if someone else is trying to interpret your code. I was taught to never do this, so I don’t. I might use the variable “area_s” inside the sub, and “area” outside, for example.
Is it common practice among Python coders to use the same variable inside/outside the sub? Do I need to change my outlook on this?
Second, I am used to having a substantial main program, followed by subroutines. In the example, the main program consists of a line or two at the end of the sub, and the meat of the code is the sub located at the top. Can I switch the order, and have the main program first and the sub second?
Hi @chartanalyst welcome to the community and thanks for your question!
I teach and have been a programmer for a while. I understand why you name your variables differently inside and outside of the subroutine to help recognise local and global variables however I’d argue it is personal preference. In some high school / college courses you would submit your program alongside a document with a variable table and perhaps even a data flow diagram too.
In answer to your second question I’ve always been taught and have taught to have subroutines at the top and main program at the end of the code. However if it works for you to reverse this to complete the 100daysofcode challenges then I would suggest you go for it!
Hope you are enjoying the challenges and keep posting to the community!
Built a character health stats generator to prepare for the epic battle in 3 days!!
Day 25 of #Replit100DaysOfCode #100DaysOfCode.
Ngl, it took me too much time and I was so stuck but finally I made through
For this challenge, the solution contains this line of code
health = str(roll_6_and_8()).
I can’t really understand the purpose of using
str, it seems the code works perfectly without
str( ) converts whatever data in the parentheses into a string.
Probably there is an implicit string conversion somewhere as with a + or a print.
So I wanted to ask why do we need the first sub-routine is it just to generate a random number that we’re not going to use other than that I mostly understood everything.(Sorry if my English isn’t that it isn’t my first language)
Are you talking about the function for dice rolling?
Am I doing the challenge correctly as the instructions ask me to? Can anyone help look at my code solution below please?: https://replit.com/@wittphoonsiri/day-25100-days-of-code#main.py
Also I am utilizing the while loop in a correct manner here? Furthermore, I don’t know how to constantly change different colors every time for new characters and health stats. (If you noticed, the new characters and health stats have the same character patterns)
import random def DiceNumber(): AnyDice = random.randint(1, 3000) print("You rolled, a", AnyDice) return AnyDice AnyDice = DiceNumber() print("there are", AnyDice, "sizes of the dice") print("⚔️ Character Stats Generator ⚔️") def HealthStats(FirstDice, SecondDice): FirstDice = random.randint(1,6) SecondDice = random.randint(1,8) Health = FirstDice * SecondDice return Health for i in range (10): Character = input("Name your warrior: ") print("\033[36m",Character) Health = HealthStats(7,5) print("\033[32m", "Their health is:", "\033[35m",Health, "hp", "\033[0m")