公路工程竣工验收程序:LAB:州际公路编号(3.23 lab: interstate highway numbers c++)

我有一个任务来编写一个程序,该程序将高速公路编号作为输入,并输出高速公路是主要还是辅助,向东 / 向西,向北 / 向南,如果它是辅助的,它服务于什么主要高速公路。

highway_number = int(input(''))
if highway_number >= 1 and highway_number <= 99:
    prim = 'is primary,'
    if (highway_number % 2) == 0:
        print('I-', highway_number, prim, 'going east/west.')
    else:
        print('I-', highway_number, prim, 'going north/south.')
elif highway_number >= 100 and highway_number <= 999:
    aux = 'is auxiliary,'
    if (highway_number % 2) == 0:
        print('I-', highway_number, aux, 'serving I- %d, going east/west.' % (highway_number%100))
    else:
        print('I-', highway_number, aux, 'serving I- %d, going north/south.' % (highway_number%100))
else:
    print(highway_number, 'is not a valid interstate highway number.')

But if I run it. It adds a space between the "I-" and the highway number. Screenshot

0

您的问题在于您打印的部分:使用print(string, string)您写出 2 个单独的刺痛,默认情况下,python 将由空格分隔。您将不得不附加字符串,这可以通过 aprint(string + string)轻松完成。一定要先使用str(top)解析数字等。

对于第二部分,在 I-和数字之间打印一个空格,因为在您的字符串中您有一个空格I- %d。Yust 将其更改为I-%d

希望这有助于:D

在完成

highway_number = int(input(''))
if highway_number >= 1 and highway_number <= 99:
    prim = 'is primary,'
    if (highway_number % 2) == 0:
        print('I-' + str(highway_number), prim, 'going east/west.')
    else:
        print('I-' + str(highway_number), prim, 'going north/south.')
elif highway_number >= 100 and highway_number <= 999:
    aux = 'is auxiliary,'
    if (highway_number % 2) == 0:
        print('I-' + str(highway_number), aux, 'serving I-%d, going east/west.' % (highway_number%100))
    else:
        print('I-' + str(highway_number), aux, 'serving I-%d, going north/south.' % (highway_number%100))
else:
    print(highway_number, 'is not a valid interstate highway number.')
0

试试这个

print('I-'+str(highway_number),prim,'going east/west.')

print('I-'+ str(highway_number),aux,'serving I-%d,going east/west.' % (highway_number%100))
0

这个答案的问题已经改变,增加了新的要求,增加了新的部分,以帮助其他人寻找相同的答案。

highway_number = int(input())
if highway_number >= 1 and highway_number <= 99:
    prim = "is primary,"
    if (highway_number % 2) == 0:
        print('I-' + str(highway_number), prim, 'going east/west.')
    else:
        print('I-' + str(highway_number), prim, 'going north/south.')
elif highway_number >= 100 and highway_number <= 999:
    aux = 'is auxiliary,'
    if highway_number % 100 == 0:
        print(highway_number, 'is not a valid interstate highway number.')
    elif (highway_number % 2) == 0:
        print('I-' + str(highway_number), aux, 'serving I-%d, going east/west.' % (highway_number%100))
    else:
        print('I-' + str(highway_number), aux, 'serving I-%d, going north/south.' % (highway_number%100))
else:
    print(highway_number, 'is not a valid interstate highway number.')

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(281)
社会代码查询全国统一:脚本定义符号代码不跳过统一 C# 中的代码
上一篇
行政区划代码6位:生成唯一的 6位代码(6 digit codes)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(39条)