The Student Room Group

Write this code in an efficient way

Is there a way to make this code more efficient by using arrays?

name1= input("Enter a name : ")
name2= input("Enter a name : ")
name3= input("Enter a name : ")
name4= input("Enter a name : ")

print ("1. "+name1)
print ("2. "+name2)
print ("3. "+name3)
print ("4. "+name4)

Please need help

Scroll to see replies

Reply 1
Original post by I-ZAAA
Is there a way to make this code more efficient by using arrays?

name1= input("Enter a name : ")
name2= input("Enter a name : ")
name3= input("Enter a name : ")
name4= input("Enter a name : ")

print ("1. "+name1)
print ("2. "+name2)
print ("3. "+name3)
print ("4. "+name4)

Please need help


Not in any particular language but I know C best, so it looks like C.

Note, arrays always start at 0.


string[4] name;

for (int i = 0; i = 3; i++)
{
name = input("Enter a name: ");
}

for (int i = 0; i = 3; i++)
{
print( (i+1) + name );
}
Reply 2
i need it in python specifically but thanks
Original post by Tootles
Not in any particular language but I know C best, so it looks like C.

Note, arrays always start at 0.


string[4] name;

for (int i = 0; i = 3; i++)
{
name = input("Enter a name: ");
}

for (int i = 0; i = 3; i++)
{
print( (i+1) + name );
}

Reply 3
Original post by I-ZAAA
i need it in python specifically but thanks

What I've written should be very easy to translate if you (a) know anything about how programming works and (b) know anything about how the language you're using works. If you don't know either but you know you need to declare, assign values to, and extract values from, an array, then it's clear that you're asking us to do your homework for you.

In which case, tuff. I've told you the basics of how arrays work. You need it in Python? Translate it.
Reply 4
Original post by Tootles
What I've written should be very easy to translate if you (a) know anything about how programming works and (b) know anything about how the language you're using works. If you don't know either but you know you need to declare, assign values to, and extract values from, an array, then it's clear that you're asking us to do your homework for you.

In which case, tuff. I've told you the basics of how arrays work. You need it in Python? Translate it.

All right, thanks
Original post by I-ZAAA
Is there a way to make this code more efficient by using arrays?

name1= input("Enter a name : ")
name2= input("Enter a name : ")
name3= input("Enter a name : ")
name4= input("Enter a name : ")

print ("1. " name1)
print ("2. " name2)
print ("3. " name3)
print ("4. " name4)

Please need help


i smell python
nameArray = *empty square brackets here*
for i in range(4):
name = input ("Enter a name : ")
nameArray.append (name)
for i in range(4):
print (i 1 , "." nameArray )

that should work
need anything explaining hmu

EDIT: idk if it shows up on pc but on mobile the brackets disappear nameArray should be followed by a pair of empty square brackets

EDIT 2: for some reason the plus sign is also disappearing :/
(edited 4 years ago)
Reply 6
Original post by budywudy9
i smell python
nameArray = [ ]
for i in range(4):
name = input ("Enter a name : ")
nameArray.append (name)
for i in range(4):
print (i 1 , "." nameArray )

that should work
need anything explaining hmu

EDIT: idk if it shows up on pc but on mobile the brackets disappear nameArray shouls be followed by a pair of empty square brackets

can you please explain what u mean by i 1 in the last line?
Reply 7
Original post by budywudy9
i smell python
nameArray = *empty square brackets here*
for i in range(4):
name = input ("Enter a name : ")
nameArray.append (name)
for i in range(4):
print (i 1 , "." nameArray )

that should work
need anything explaining hmu

EDIT: idk if it shows up on pc but on mobile the brackets disappear nameArray should be followed by a pair of empty square brackets

EDIT 2: for some reason the plus sign is also disappearing :/

Ah okay the
+ was missing
Original post by I-ZAAA
can you please explain what u mean by i 1 in the last line?


i 1 is supposed to be i plus 1 but for some reason the plus sign is disappearing :frown:
same for "." nameArray [i ] it should be "." plus nameArray [i ]
Reply 9
an index error is occuring, the list is out of range
Original post by budywudy9
i 1 is supposed to be i plus 1 but for some reason the plus sign is disappearing :frown:
same for "." nameArray [i ] it should be "." plus nameArray [i ]
Original post by Tootles
Not in any particular language but I know C best, so it looks like C.

Note, arrays always start at 0.


string[4] name;

for (int i = 0; i = 3; i++)
{
name = input("Enter a name: ");
}

for (int i = 0; i = 3; i++)
{
print( (i+1) + name );
}

That's not 'C'.
Original post by RogerOxon
That's not 'C'.

I said it looks like C, not it is C. As in I used C's formatting, but wrote pseudocode.
(edited 4 years ago)
Reply 12
GOT IT. It works thank you

Original post by budywudy9
i 1 is supposed to be i plus 1 but for some reason the plus sign is disappearing :frown:
same for "." nameArray [i ] it should be "." plus nameArray [i ]
It's my flavour of pseudocode, which looks like C because C's my native language.
Original post by I-ZAAA
an index error is occuring, the list is out of range


https://imgur.com/a/d98wPcj
this is what it should look like
this definitely works, so sorry if the formatting on here has made my code even more confusing
It really wouldn't. I didn't format my inputs or outputs or use the correct input or output functions. And on top of that, you can't have an array of strings in C, because C doesn't have strings - you'd use a pointer to an array of chars.
Original post by Tootles
I said it looks like C, not it is C. As in I used C's formatting, but wrote pseudocode.

Fair enough. You can't overwrite name like that though. Using a signed type for an array index is a little odd too. I know - I'm a pedant.
Original post by RogerOxon
Fair enough. You can't overwrite name like that though. Using a signed type for an array index is a little odd too. I know - I'm a pedant.

You're damn' skippy. It's pseudocode. It doesn't have to have syntactic rigour, it only has to get the point across. If I did it that wrong, why don't you rewrite it?
Original post by Tootles
You're damn' skippy. It's pseudocode. It doesn't have to have syntactic rigour, it only has to get the point across. If I did it that wrong, why don't you rewrite it?

Too busy writing real code, but criticising is quick :wink:
Original post by RogerOxon
Too busy writing real code, but criticising is quick :wink:

Writing js scripts to download content from PornHub doesn't count.

Quick Reply

Latest

Trending

Trending