natho49
Badges: 0
Rep:
?
#1
Report Thread starter 7 years ago
#1
How do you times to different values from two different lists this is what I got but it dont work:import sys
m=[11,10,9,8,7,6,5,4,3,2]# assings all the mutiplies to a list
l=[]#creates an empty list
h=[]#creates an empty list
k=[]
final_number=[]#creates an empty list
def isbn():
"""This is the main program."""
while True:
num=input('Please enter the number. Enter 0 to quit')# asks for the number(as a string)
if num=='Q':
sys.exit()
if len(num)==10 and num.isdigit() checks whether the input is ten digits and checks if it is a digit, if both of this statements are ture then the following code is ru.
for element in num:
k.append(int(element))
print(k)

count=11# assigns 11 to count- to make a counter.
for eachChar in l[0] a for loop. The following code will be run for evry character in l[0]
eachChar=int(eachChar)#it turns the character in to an integer
total=eachChar* count# mutiplies the character and the mutiple that it needs to times by(e.g.11)
h.append(total)#the total is added to the final_number list
count=count-1# counter
0
reply
Puddles the Monkey
Badges: 21
Rep:
?
#2
Report 7 years ago
#2
Just moved this to the computer science forum for you, some people might be able to give you a response here.
0
reply
Push_More_Button
Badges: 10
Rep:
?
#3
Report 7 years ago
#3
FYI, putting the code in CODE tags makes it much easier to read.
Code:
import sys
m=[11,10,9,8,7,6,5,4,3,2]# assings all the mutiplies to a list
l=[]#creates an empty list
h=[]#creates an empty list
k=[]
final_number=[]#creates an empty list
def isbn():
    """This is the main program."""
    while True:
        num=input('Please enter the number. Enter 0 to quit')# asks for the number(as a string)
        if num=='Q':
            sys.exit()
        if len(num)==10 and num.isdigit():# checks whether the input is ten digits and checks if it is a digit, if both of this statements are ture then the following code is ru.
            for element in num:
                k.append(int(element))
            print(k)

           count=11# assigns 11 to count- to make a counter.
            for eachChar in l[0]:# a for loop. The following code will be run for evry character in l[0]
                eachChar=int(eachChar)#it turns the character in  to an integer
                total=eachChar* count# mutiplies the character and the mutiple that it needs to times by(e.g.11)
                h.append(total)#the total is added to the final_number list
                count=count-1# counter
What is your problem exactly? Which parts are you struggling with? Simply saying "it dont work" doesn't help me and I'm not going to waste my time trying to read your mind if you can't be bothered to spend a bit of time explaining yourself.

Having said that:
Line 10 - Your input won't necessarily be a string. Python will automatically try to interpret it's type when you enter it like so
Code:
>>> test1 = input("Enter something")Enter something12
>>> print type(test1)
<type 'int'>
>>> test2 = input("Enter something")
Enter something"hello"
>>> print type(test2)
<type 'str'>
>>> test3 = input("Enter something")
Enter somethinghello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
As you can see, it recognised entering a number and took it as an int, it recognised a string and then failed for a non-existant object.


With this in mind it might make line 13 easier.
0
reply
Planto
Badges: 16
Rep:
?
#4
Report 7 years ago
#4
(Original post by Push_More_Button)
Line 10 - Your input won't necessarily be a string.
It will in Python 3.x AFAIK. input replaces raw_input and the old input is gone (which is probably a good thing because doing eval() on a user-input string is all kinds of wrong).
0
reply
Push_More_Button
Badges: 10
Rep:
?
#5
Report 7 years ago
#5
(Original post by Planto)
It will in Python 3.x AFAIK. input replaces raw_input and the old input is gone (which is probably a good thing because doing eval() on a user-input string is all kinds of wrong).
Whoops, you're absolutely right. I should have spotted it was Python3 with print(k)...

In that case, natho49, ignore my comment on that but it goes to show you need to help us help you...
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Poll: What factors affect your mental health most right now? Post-lockdown edition

Anxiousness about restrictions easing (54)
6.07%
Uncertainty around my education (101)
11.35%
Uncertainty around my future career prospects (94)
10.56%
Lack of purpose or motivation (110)
12.36%
Lack of support system (eg. teachers, counsellors, delays in care) (44)
4.94%
Impact lockdown had on physical health (48)
5.39%
Social worries (incl. loneliness/making friends) (99)
11.12%
Financial worries (60)
6.74%
Concern about myself or my loves ones getting/having been ill (40)
4.49%
Exposure to negative news/social media (56)
6.29%
Difficulty accessing real life entertainment (26)
2.92%
Lack of confidence in making big life decisions (86)
9.66%
Worry about missed opportunities during the pandemic (72)
8.09%

Watched Threads

View All