楼主: 王贴贴123456
3138 3

[问答] Python初学者 冒号报错 [推广有奖]

  • 10关注
  • 5粉丝

博士生

59%

还不是VIP/贵宾

-

威望
0
论坛币
146 个
通用积分
5.3500
学术水平
0 点
热心指数
4 点
信用等级
0 点
经验
4690 点
帖子
187
精华
0
在线时间
357 小时
注册时间
2012-9-24
最后登录
2024-8-15

楼主
王贴贴123456 在职认证  发表于 2017-4-10 20:27:16 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. # -*- coding: utf-8 -*-
  2. states = [
  3.     'oregon': 'or',    运行时老是提示,冒号错误,是在英文半角下写的,但是还是出错,求解
  4.     'florida':'fl',
  5.     'california':'ca',
  6.     'new york':'ny'
  7.    
  8. ]

  9. cities = [
  10.     'ca':'sanfrancisco'
  11.     'mi':'detroit'
  12.     'fl':'jackonvile'

  13. ]

  14. cities['ny'] = 'new york'
  15. cities['or'] = 'portland'

  16. print '-' * 10
  17. print 'ny state has:',cities['ny']
  18. print 'or state has:',cities['or']

  19. print '-'*10
  20. print 'michigan abbreviation is :',states['michigan']
  21. print 'florida abbreviation is :',states['florida']

  22. print '-'*10
  23. print "michigan has:",cities[states['michigan']]
  24. print "florida has ",cities[states['florida']]

  25. print '-' * 10
  26. for state,abbrev in states.item():
  27.     print "%s is abbreviated %s" %(state,abbrev)

  28. print '-' * 10
  29. for state,abbrev in states.items():
  30.     print "%s state is abbreviated %s and has city %s"   %(state,abbrev,cities[abbrev])

  31. print '-' * 10
  32. state = states.get('texas',none)

  33. if not state:
  34.     print "sorry ,no texas"

  35. city = cities.get('tx','does not exist')
  36. print "the city for the state 'tx' is :%s"%city




  37. 报错提示# -*- coding: utf-8 -*-
  38. states = [
  39.     'oregon': 'or',
  40.     'florida':'fl',
  41.     'california':'ca',
  42.     'new york':'ny'
  43.    
  44. ]

  45. cities = [
  46.     'ca':'sanfrancisco'
  47.     'mi':'detroit'
  48.     'fl':'jackonvile'

  49. ]

  50. cities['ny'] = 'new york'
  51. cities['or'] = 'portland'

  52. print '-' * 10
  53. print 'ny state has:',cities['ny']
  54. print 'or state has:',cities['or']

  55. print '-'*10
  56. print 'michigan abbreviation is :',states['michigan']
  57. print 'florida abbreviation is :',states['florida']

  58. print '-'*10
  59. print "michigan has:",cities[states['michigan']]
  60. print "florida has ",cities[states['florida']]

  61. print '-' * 10
  62. for state,abbrev in states.item():
  63.     print "%s is abbreviated %s" %(state,abbrev)

  64. print '-' * 10
  65. for state,abbrev in states.items():
  66.     print "%s state is abbreviated %s and has city %s"   %(state,abbrev,cities[abbrev])

  67. print '-' * 10
  68. state = states.get('texas',none)

  69. if not state:
  70.     print "sorry ,no texas"

  71. city = cities.get('tx','does not exist')
  72. print "the city for the state 'tx' is :%s"%city
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python 初学者 california Francisco Michigan Python 初学

回帖推荐

飞天玄舞6 发表于2楼  查看完整内容

states和cities是字典吧,应该是花括号{}。

沙发
飞天玄舞6 在职认证  发表于 2017-4-10 21:56:13
states和cities是字典吧,应该是花括号{}。
已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
残阳_等待 + 20 + 1 精彩帖子
客初 + 2 + 5 + 2 热心帮助其他会员

总评分: 经验 + 20  学术水平 + 2  热心指数 + 6  信用等级 + 2   查看全部评分

藤椅
王贴贴123456 在职认证  发表于 2017-4-12 20:39:24
飞天玄舞6 发表于 2017-4-10 21:56
states和cities是字典吧,应该是花括号{}。
谢谢,问题解决了。

板凳
benjobvatlonhoy 发表于 2017-4-21 14:59:08
提问的时候最好可以贴出错误报错提示

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-7 23:09