Putting variables in the lists in Python -
i ask how can use variables define size of array in python (i mean list).i have written piece of code below , please let me know wrong code? thanks..
elif(op=='+') : size=int(input("please enter how many numbers want add")) x in range(0,size): print("please enter number",x+1) inp=(input()) num[x]=inp #<<<-----the error comes when trying run expression z in range(0,size): num[z]=num[z]+num[z+1] print("the result " , num[size])
besides problem in inp @daniel answers, result of input() string. thus, code concatenate numbers go through list. additionally, why putting concatenation list? when going range, appending entered number of digits, index operation goes 0 index "size" index 1 more in list.
z in range(0,size): num[z]=num[z]+num[z+1] print("the result " , num[size]) thus when z == size -1, index out of range when attempting reference num[z+1] in final print tries reference num[size]
additionally, if going adding rather concatenating input string, should saying inp = int(input())
size=int(input("please enter how many numbers want add")) mytotal = 0 x in range(0,size): # next 2 have been on 1 line myval = int(input("please enter number")) mytotal += myval #this split clarity print mytotal
Comments
Post a Comment