在Python中,我試圖構建一個函數,它將在一行上打印一個字母和一系列隨機數。打印的行數將在1-10之間,取決於用戶的輸入。最終結果應該是這樣的:如何打印遞增字符
A 10 11 15 20 40 (actual numbers will vary, between 1-69)
B 12 15 19 30 45
etc..
我的問題是獲取要打印的第一個字符。我似乎無法找到一個方法來增加字符,以便下一行是B,之後,下一個是C等
以下是我有:
def generateLetter():
value = 'A'
newVar = (chr(ord(value[0])+1))
value = newVar
print(value)
def main():
howMany = input('print how many lines?')
count = 0
while howMany > 10:
if count == 3:
break;
count +=1
if howMany <=10:
print ('now printing ', howMany, 'lines')
for each in range (howMany):
generateLetter(),
#another function to generate random string of numbers
目前,以上結果如下:
B
5 6 7 8 9
B
10 11 12 13 14
etc...
我在做什麼錯?
您是否檢查過「value」的值? –
每次調用'generateLetter'時,都會使'value'開始於'A''。您需要保持呼叫之間的狀態。 – BallpointBen