0

Im trying to use %d inside of a variable so it can change.

For example, when the variable is "horse1" the "1" can be changed to 2, 3, 4 etc. to make the variable "horse2, horse3, and horse4 etc.

This is a small line out of a long code that im trying to shorten

horseno = 1
horse1 = 4

if horse%d > 1 %(horseno):
     print("nice")

What im trying to do is make it say this, but just in a way where i can change the horse number.

horseno = 1
horse1 = 4

if horse1 > 1:
     print("nice")

However, when I run the first code shown, there is an error. Is there any way I can fix this?

NNBananas
  • 115
  • 6
  • 2
    Just use a list: `horses[1]` instead of `horse1`. Then you can do `horses[horseno]` to get an arbitrary horse. – Fractalism Jan 25 '23 at 11:29
  • Why don't you use `dict` instead of multiple variables? – Harun Yilmaz Jan 25 '23 at 11:29
  • Sorry @Fractalism, I don't really understand. Could you apply your changes to the real line of code please if you don't mind? – NNBananas Jan 25 '23 at 14:59
  • `%d` is a special sequence in a string interpreted by `str.__mod__`; it is not fundamental syntax that can be applied to anything you want. – chepner Jan 25 '23 at 15:07
  • `horse%d` is just the expression`horse % d`, and you have defined neither `horse` nor `d`; `horse` is just the first undefined name it encounters, so that's why it's the error that gets reported. – chepner Jan 25 '23 at 15:08
  • @NicholasChan Sorry but the question has been closed. You could read up on "array" as a general concept and "list" for its equivalent in python. – Fractalism Jan 25 '23 at 15:24

0 Answers0