Write this code in an efficient way Watch

I-ZAAA
Badges: 12
Rep:
?
#1
Report Thread starter 4 weeks ago
#1
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
0
reply
Tootles
Badges: 20
#2
Report 4 weeks ago
#2
(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.

Code:
string[4] name;

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

for (int i = 0; i = 3; i++)
{
  print( (i+1) + name[i] );
}
0
reply
I-ZAAA
Badges: 12
Rep:
?
#3
Report Thread starter 4 weeks ago
#3
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.

Code:
string[4] name;

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

for (int i = 0; i = 3; i++)
{
  print( (i+1) + name[i] );
}
0
reply
Tootles
Badges: 20
#4
Report 4 weeks ago
#4
(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.
1
reply
I-ZAAA
Badges: 12
Rep:
?
#5
Report Thread starter 4 weeks ago
#5
(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
0
reply
budywudy9
Badges: 4
Rep:
?
#6
Report 4 weeks ago
#6
(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 [i])

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 :/
Posted on the TSR App. Download from Apple or Google Play
Last edited by budywudy9; 4 weeks ago
1
reply
I-ZAAA
Badges: 12
Rep:
?
#7
Report Thread starter 4 weeks ago
#7
(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 [i])

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?
0
reply
I-ZAAA
Badges: 12
Rep:
?
#8
Report Thread starter 4 weeks ago
#8
(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 [i])

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
0
reply
ltsmith
Badges: 20
Rep:
?
#9
Report 4 weeks ago
#9
each stdin and stdout can be generalised for any i and put into a loop
1
reply
budywudy9
Badges: 4
Rep:
?
#10
Report 4 weeks ago
#10
(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
same for "." nameArray [i ] it should be "." plus nameArray [i ]
Posted on the TSR App. Download from Apple or Google Play
0
reply
I-ZAAA
Badges: 12
Rep:
?
#11
Report Thread starter 4 weeks ago
#11
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
same for "." nameArray [i ] it should be "." plus nameArray [i ]
0
reply
RogerOxon
Badges: 21
Rep:
?
#12
Report 4 weeks ago
#12
(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.

Code:
string[4] name;

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

for (int i = 0; i = 3; i++)
{
  print( (i+1) + name[i] );
}
That's not 'C'.
Posted on the TSR App. Download from Apple or Google Play
0
reply
Tootles
Badges: 20
#13
Report 4 weeks ago
#13
(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.
Last edited by Tootles; 4 weeks ago
0
reply
ltsmith
Badges: 20
Rep:
?
#14
Report 4 weeks ago
#14
(Original post by RogerOxon)
That's not 'C'.
what is it then?
0
reply
I-ZAAA
Badges: 12
Rep:
?
#15
Report Thread starter 4 weeks ago
#15
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
same for "." nameArray [i ] it should be "." plus nameArray [i ]
0
reply
Tootles
Badges: 20
#16
Report 4 weeks ago
#16
(Original post by ltsmith)
what is it then?
It's my flavour of pseudocode, which looks like C because C's my native language.
0
reply
budywudy9
Badges: 4
Rep:
?
#17
Report 4 weeks ago
#17
(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
Posted on the TSR App. Download from Apple or Google Play
0
reply
ltsmith
Badges: 20
Rep:
?
#18
Report 4 weeks ago
#18
(Original post by Tootles)
It's my flavour of pseudocode, which looks like C because C's my native language.
i see. even though it's your pseudocode, i think it would still be acceptable C (given that an input method is defined and you change = to <)
0
reply
Tootles
Badges: 20
#19
Report 4 weeks ago
#19
(Original post by ltsmith)
i see. even though it's your pseudocode, i think it would still be acceptable C (given that an input method is defined and you change = to <)
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.
0
reply
RogerOxon
Badges: 21
Rep:
?
#20
Report 4 weeks ago
#20
(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.
Posted on the TSR App. Download from Apple or Google Play
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
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

University open days

  • Bournemouth University
    Clearing Open Day Undergraduate
    Wed, 31 Jul '19
  • Staffordshire University
    Postgraduate open event - Stoke-on-Trent campus Postgraduate
    Wed, 7 Aug '19
  • University of Derby
    Foundation Open Event Further education
    Wed, 7 Aug '19

Are you tempted to change your firm university choice on A-level results day?

Yes, I'll try and go to a uni higher up the league tables (156)
17.63%
Yes, there is a uni that I prefer and I'll fit in better (76)
8.59%
No I am happy with my course choice (526)
59.44%
I'm using Clearing when I have my exam results (127)
14.35%

Watched Threads

View All