簡體   English   中英

Python,谷歌colab

[英]Python, google colab

x=str(input('Where to'))
y=str(input('Which uber'))

if x == ('Cleveland'):
  print('400 miles')
if x == ('Detriot'):
  print('500 miles')
if x == ('Sandusky'):
  print('100 miles')
if x == ('Lakewood'):
  print('200 miles')
if x == ('Rocky River'):
    print('550 miles')
elif x == ('none'):
      print(compares)
compares = x * y

問題:如果我讓用戶為 x 輸入一個新值,我將如何將其乘以他們給出的 y?

你可以用字典

注意:默認情況下input()返回字符串,無需將其轉換為字符串。

x = input('Where to: ')
y = input('Which uber: ')
cities = {'Cleveland': 400, 'Detriot': 500, 'Sandusky': 100, 'Lakewood': 200, 'Rocky River': 550}

if x in cities and y in cities:
    compares = cities[x] * cities[y]
    print(compares)
else:
    print('Select valid location')

暫無
暫無

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

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