一个不是加拿大的国家

时间:2015-07-09 20:13:16

标签: python if-statement

对于我在python中的代码。如果这个国家不是加拿大,那么我如何才能这样做呢?只需要打印total_price而不加税?现在,如果我把美国给它给我合适的价格,但它也给了我未提及的其他省份的价格。

country = raw_input('What country are you from? ').lower()
if country == 'canada':
    total_price = int(raw_input('What was your total price? '))
    province = raw_input('What province are you from? ').lower()
elif country != 'canada':
    total_price = int(raw_input('What was your total price? '))
if province == 'alberta':
    total_alberta = (total_price * .00005) + total_price
    print 'Your total price is ' + str(total_alberta)
if province == 'ontario' or province == 'new brunswick'\
or province == 'nova scotia':
    total_onn = (total_price * .0013) + total_price
    print 'Your total price is ' + str(total_onn)
if country == 'canada' and province != 'ontario' and province != 'new brunswick' and province != 'nova scotia' and province != 'alberta':
    total_else = ((total_price * .0006) + (total_price * .0005)) \
    + total_price
    print 'Your total price is ' + str(total_else)
else:
    print 'Your total price is ' + str(total_price)

5 个答案:

答案 0 :(得分:2)

干净和pythonic版本 - 您的逻辑和ifs嵌套不良:

base_canada_tax = 0.13
provinces = {'alberta': 0.05, 'ontario': base_canada_tax, 'new brunswick': base_canada_tax, 'nova scotia': base_canada_tax}

country = raw_input('What country are you from? ').lower()
total_price = int(raw_input('What was your total price? '))
if country == 'canada':
    province_in = raw_input('What province are you from? ').lower()
    total_price *=  1 + provinces.get(province_in, base_canada_tax)
print 'Your total price is {0}'.format(total_price)

答案 1 :(得分:0)

将代码放在第一个if:

country = raw_input('What country are you from? ').lower()
if country == 'canada':
    total_price = int(raw_input('What was your total price? '))
    province = raw_input('What province are you from? ').lower()
    if province == 'alberta':
        total_alberta = (total_price * .00005) + total_price
        print 'Your total price is ' + str(total_alberta)
    elif province == 'ontario' or province == 'new brunswick'\
    or province == 'nova scotia':
        total_onn = (total_price * .0013) + total_price
        print 'Your total price is ' + str(total_onn)
    else:
        total_else = ((total_price * .0006) + (total_price * .0005)) \
        + total_price
        print 'Your total price is ' + str(total_else)
else:
    total_price = int(raw_input('What was your total price? '))
    print 'Your total price is ' + str(total_price)

答案 2 :(得分:0)

将省逻辑嵌入if country == "canada"

country = raw_input('What country are you from? ').lower()
total_price = int(raw_input('What was your total price? '))
if country == 'canada':
    province = raw_input('What province are you from? ').lower()
    if province == 'alberta':
        total_price= (total_price * .00005) + total_price
    elif province in ['ontario', 'new brunswick', 'nova scotia']:
        total_price= (total_price * .0013) + total_price
print 'Your total price is ' + str(total_price)

你的布尔逻辑在许多情况下也是多余的,可以像我上面那样进行修剪

答案 3 :(得分:0)

将您的if province声明置于if canada内,然后如果该国家不是加拿大则会将其跳出整个if if statment

if country =='canada':
    if province =='ontario':
    .....
else //the not canada statment

答案 4 :(得分:0)

这是基于@ Incognos的回答,更正了逻辑和数字:

provinces = {'alberta': 1.05, 'ontario': 1.13,
             'new brunswick': 1.13, 'nova scotia': 1.13} # excess alberta removed
base_canada_tax = 1.113 # 6% and 5% together as per question's (corrected) code
country = raw_input('What country are you from? ').lower()
total_price = float(raw_input('What was your total price? ')) # float
if country == 'canada':
    province = raw_input('What province are you from? ').lower()
    total_price *= provinces.get(province, base_canada_tax)
print 'Your total price is', total_price