(I think I’ve worded the question correctly but am not sure…)
I’m trying to create a loop that replaces the number in the instance so that I only have to write the print statement once instead of once for each instance. What I’ve got below does that but it’s creating a string and that string is what gets returned instead of the instance variable:
import random class Dog: info = "furry critter" def __init__(self, name): self.dogName = name self.luckyNum = random.randint(1,10) dog1 = Dog("Ruth") dog2 = Dog("Chowder") dog3 = Dog("King") #Trying to replace this: print("My name is", dog1.dogName, "My lucky number is", dog1.luckyNum) print("My name is", dog2.dogName, "My lucky number is", dog2.luckyNum) print("My name is", dog3.dogName, "My lucky number is", dog3.luckyNum) # With this: x = 1 while x < 3: objectNum = f"dog{x}" print(objectNum) dogName = f"{objectNum}.dogName" print (dogName) luckyNum = f"{objectNum}.luckyNum" print (luckyNum) print(f"My name is", dogName, "My lucky number is", luckyNum) x += 1
Thanks.
submitted by /u/DurmNative
[link] [comments]
r/learnpython (I think I’ve worded the question correctly but am not sure…) I’m trying to create a loop that replaces the number in the instance so that I only have to write the print statement once instead of once for each instance. What I’ve got below does that but it’s creating a string and that string is what gets returned instead of the instance variable: import random class Dog: info = “furry critter” def __init__(self, name): self.dogName = name self.luckyNum = random.randint(1,10) dog1 = Dog(“Ruth”) dog2 = Dog(“Chowder”) dog3 = Dog(“King”) #Trying to replace this: print(“My name is”, dog1.dogName, “My lucky number is”, dog1.luckyNum) print(“My name is”, dog2.dogName, “My lucky number is”, dog2.luckyNum) print(“My name is”, dog3.dogName, “My lucky number is”, dog3.luckyNum) # With this: x = 1 while x < 3: objectNum = f”dog{x}” print(objectNum) dogName = f”{objectNum}.dogName” print (dogName) luckyNum = f”{objectNum}.luckyNum” print (luckyNum) print(f”My name is”, dogName, “My lucky number is”, luckyNum) x += 1 Thanks. submitted by /u/DurmNative [link] [comments]
(I think I’ve worded the question correctly but am not sure…)
I’m trying to create a loop that replaces the number in the instance so that I only have to write the print statement once instead of once for each instance. What I’ve got below does that but it’s creating a string and that string is what gets returned instead of the instance variable:
import random class Dog: info = "furry critter" def __init__(self, name): self.dogName = name self.luckyNum = random.randint(1,10) dog1 = Dog("Ruth") dog2 = Dog("Chowder") dog3 = Dog("King") #Trying to replace this: print("My name is", dog1.dogName, "My lucky number is", dog1.luckyNum) print("My name is", dog2.dogName, "My lucky number is", dog2.luckyNum) print("My name is", dog3.dogName, "My lucky number is", dog3.luckyNum) # With this: x = 1 while x < 3: objectNum = f"dog{x}" print(objectNum) dogName = f"{objectNum}.dogName" print (dogName) luckyNum = f"{objectNum}.luckyNum" print (luckyNum) print(f"My name is", dogName, "My lucky number is", luckyNum) x += 1
Thanks.
submitted by /u/DurmNative
[link] [comments]