Off Topic > Off Topic
Programming Megathread
BluetoothBoy:
Disclaimer: I don't actually do python. :cookieMonster:
Basically you just want to check the currently generated value of name2 against all the other values of name2. If it encounters a match, it increases a counter. After looping through all the values, whatever the counter is at is appended to the end of the name in Roman numeral format.
--- Code: ---import random
name1 = ['Suleiman',
'Osman',
'Bayezid',
'Mehmed',
'Orhan',
'Murad',
'Selim']
name2 = []
name3 = [' the Magnificent',
' the Lawgiver',
' the Conqueror',
' the Terrible',
' the Stammerer',
' the Fair',
' the Great',
' the Silent']
#Variable i started at -1 instead of 0 since the second for loop is guaranteed to run into it's own value at least once (i.e. name2[x] == name2[z] if x and z are 3 -> name2[3] == name2[3] will always be true).
#We could put an extra if here to take care of that, but it only increases the number of calculations the computer has to do every loop. There are other ways to do it as well, but this code is simple enough to have this work just fine.
i = -1
roman = [' I', ' II', ' III', ' IV', ' V', ' VI', ' VII', ' VIII', ' IX', ' X']
name4 = []
for x in range(10):
name2.append(random.choice(name1))
for z in name2:
if name2[x] == name2[z]:
i++
#Use new variable to avoid confusing our array comparison.
name4[x] = 'Sultan ' + name2[x] + roman[i]
i = -1
for y in range(3):
name2[random.randint(0, 9)] = random.choice(name4) + random.choice(name3)
print('\n'.join(name2))
--- End code ---
Untested, but it should work. Let me know if something doesn't.
Headcrab Zombie:
loving QuickBooks Online API
Whoever wrote this stuff doesn't have enough English skills to give descriptive error messages that makes sense
"Can not instanti specified is unsupported or invalid"
pls
Tudoreleu:
oh stuff i didnt know this thread was a thing
did we establish which of the following the blf prefers?
--- Code: ---stuff {
more stuff
}
--- End code ---
or
--- Code: ---stuff
{
more stuff
}
--- End code ---
as well as tabs vs spaces and vim vs emacs
Foxscotch:
--- Quote from: BluetoothBoy on January 25, 2016, 11:38:16 PM ---Untested, but it should work. Let me know if something doesn't.
--- End quote ---
there is no increment operator in python, you have to use x += 1 instead
also, for loops in python give you the item in the list, not the indices. so instead of for z in name2 you'd have to use for z in range(len(name2)), where range is a function that returns a generator that generates a "list" using integers with a length equal to the argument, which in this case is the length of name2
so range(10) generates "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
range() also has more functionality but that's irrelevant right now
one problem (??) with your code jairo is that the last for loop picks a random name from name2, which, as it turns out, can include a name that has already had one of the suffixes appended to it. which means you can get names like "Sultan Selim the Lawgiver the Great"
but idk, maybe that's intentional?
anyway here
http://pastebin.com/N5H6Ybcz
I included a function somebody else made for the part that actually converts a number to a roman numeral
besides what you asked for, I also changed a few other things to make it better (sometimes subjectively):
more descriptive variable names
in the for loops, changed the letters to i, j, and k
shortened the contents of the first loop
speaking of for loops, instead of adding the roman numeral bit to the first one, it does that in a second loop. the loop goes backwards, because that was the easiest way I could think of for doing it
and fixed the problem with the last loop that I mentioned before, where you'd get repeats. my edits will not allow more than one suffix to be added to a single name, nor will it allow one suffix to be used more than once. if that was intentional, it should be easy to switch either part back. if you want both to be the same I also left the original code at the top of the loop, but commented. the only changes I made to it were selecting a name considering the length of the list and splitting it into two lines to keep it under 80 characters long
I think that's all of it
here's some example output:
Sultan Suleiman
Sultan Selim
Sultan Orhan
Sultan Bayezid
Sultan Bayezid II the Silent
Sultan Bayezid III the Lawgiver
Sultan Orhan II
Sultan Bayezid IV the Great
Sultan Selim II
Sultan Bayezid V
clearly, the Bayezids were exceptional leaders
--- Quote from: Tudoreleu on January 26, 2016, 10:41:37 AM ---did we establish which of the following the blf prefers?
--- End quote ---
I prefer the first bracket to be on the same line that it starts on, like the first one
also spaces are better. I don't know of any reason to use tabs intentionally
Tudoreleu:
for me it's { on the same line, tabs (spaces are dumb), and neither because i'm on windows so i just use atom