Fellow Python developers,

Python has a sorted() or sort() function to implement ascending or descending order programs,but supposing if i want to develop a ascending order program that takes user input and ascends or descends them,how should I go about?

For Example: Lets Say i want to convert this C program to Python?

# include <iostream.h>
# include <conio.h>
void main()
{   
int a[10],i,j,n,temp;
clrscr();
cout<<"\nEnter n nos: ";
cin>>n;
cout<<"\nEnter the numbers: ";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];

a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n;i++)
cout<<a[i]<<endl;
getch();
}

This is an ascending order program in C.

Thanks in Advance!

Recommended Answers

All 4 Replies

Member Avatar for Enalicho

Well, converting that piece of code from C++ to Python is quite easy. Why not give it a go yourself, then post it alongside what you can't manage to do.

You dont need to convert code just write it in python.
I could of course convert it to look exactly like C++ over,but that had been stupid.
Because python dos most stuff a lot eaiser than C/C++.

print 'Enter numers,use q to stop'
l = []
while True:
    user_input = raw_input('Enter numers: ')
    if user_input == 'q':
        break
    else:
        l.append(user_input)

num_list = (float(i) for i in l)
#Ascending order
print sorted(num_list)
#Descending order
#print sorted(num_list, reverse=True)
Member Avatar for Enalicho

You dont need to convert code just write it in python.
I could of course convert it to look exactly like C++ over,but that had been stupid.
Because python dos most stuff a lot eaiser than C/C++.

print 'Enter numers,use q to stop'
l = []
while True:
    user_input = raw_input('Enter numers: ')
    if user_input == 'q':
        break
    else:
        l.append(user_input)

num_list = (float(i) for i in l)
#Ascending order
print sorted(num_list)
#Descending order
#print sorted(num_list, reverse=True)

What you've done there IS converting it to Python. ;)

if china = corrupt:
har har
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.