在String中引用Integer?蟒蛇

时间:2015-06-21 02:16:10

标签: python for-loop

如何使用python引用字符串中的整数?我对编码完全不熟悉,我正在尝试进行此错误收集练习,用户将输入每周收集的错误数量一周,并显示在本周末收集的错误总数。

这是我到目前为止的代码。

totalBugs = 0.0
day = 1

for day in range(7):
    bugsToday = input('How many bugs did you get on day', day,'?')
    totalBugs = totalBugs + bugsToday

print 'You\'ve collected ', totalBugs, ' bugs.'

所以我试图在循环中获取bugsToday提示来询问用户 "你在第1天收集了多少个错误?" "你在第2天收集了多少个错误?" 等等。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

我个人非常喜欢format()。您可以编写代码:

totalBugs = 0
for day in range(1, 8):
   bugsToday = raw_input('How many bugs did you get on day {} ?'.format(day))
   totalBugs += int(bugsToday)

print 'You\'ve collected {} bugs.'.format(totalBugs)

range(1, 8)会经历day = 1day = 7,如果这是您想要做的事情。

答案 1 :(得分:1)

您可以尝试

!is_container<T>::value

答案 2 :(得分:1)

我这样做的方式如下。

ERROR:  there is no unique constraint matching given keys for referenced table "bills"

阅读字符串格式:http://www.learnpython.org/en/String_Formatting

如果您对此感兴趣,我写了关于raw_input与输入的文章:https://medium.com/@GallegoDor/python-exploitation-1-input-ac10d3f4491f

来自Python文档:

  

CPython实现细节:如果s和t都是字符串,某些Python实现(如CPython)通常可以对s = s + t或s + = t形式的赋值执行就地优化。如果适用,此优化使得二次运行时间的可能性大大降低。此优化依赖于版本和实现。对于性能敏感的代码,最好使用str.join()方法,该方法确保跨版本和实现的一致的线性串联性能。

一开始编程看起来势不可挡,只要坚持下去就不会后悔。祝我的朋友好运!