簡體   English   中英

不明白為什么我出現索引錯誤

[英]Don't understand why I'm getting an index error

不明白為什么我得到元組錯誤超出范圍。 請幫忙


contactname_1 = input('Enter the 1st contact name:')
contactnum_1 = input('Enter the 1st contact phone number: ')
contactmail_1 = input('Enter the 1st contact email: ')

contactname_2 = input('Enter the 2nd contact name: ')
contactnum_2 = input('Enter the 2nd contact phone number: ')
contactmail_2 = input('Enter the 2nd contact email: ')

Display_align = input('Enter display alignment left/right/center (L/R/C)?')

if (Display_align == 'L'):
  print('{0:<30} {1:<30} {2:<30}'.format('Name' + 'Phone' + 'Email'))
  print('{0:<30} {1:<30} {2:<30}'.format(contactname_1, contactnum_1, contactmail_1))
  print('{0:<30} {1:<30} {2:<30}'.format(contactname_2, contactnum_2, contactmail_2))

將字符串作為args傳遞給格式時,可以將它們串聯起來。
這是更正的版本:

print('{0:<30} {1:<30} {2:<30}'.format('Name', 'Phone', 'Email'))

在第一個print語句中,您需要使用format方法的三個元組。 添加“名稱”,“電話”和“電子郵件”會得到一個字符串。 更換

print('{0:<30} {1:<30} {2:<30}'.format('Name' + 'Phone' + 'Email'))

print('{0:<30} {1:<30} {2:<30}'.format('Name', 'Phone','Email'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM