Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with this code
#1
Im kinda a newbie with programation.
i've been asked to make a program which generates all the possible ASCII combinations up to 8 characters.
ive been working with c++ but i'd like to learn python. so how would this look in python language?

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
main(){
int siz;
printf("password lenght?");
scanf("%i",&tam);

if (siz==1){
for (int i=0; i<256;i++){
printf("%c\n",i);
}
}
if(siz==2){
for(int i=0; i<256;i++){
for(int j=0; j<256;j++)
printf("%c%c\n",i,j);
}
}
}

its just repeating those cycles until siz reaches 8
Reply
#2
Please use code or python tags when posting code, see the BBCode link in my signature, below.

A for loop in Python is very simple:

for i in range(256):
    print(i)
The chr function will convert an int to str. Depending on the restrictions on your homework assignment you might want to look at the itertools module, or consider doing this with recursion (so you don't have to write out all of the loops).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
print ("password lenght")
l = input()
if (l==1):
    for i in range (256):
        print (i)
when i put the number 1 it just prints 1 and not the whole list.
What's wrong here?
Reply
#4
Don't see where is the print(l) function to print the input.
The input() is a string so comparing string with an integer in the if statement is wrong. You have either to turn the input into an integer or to test it against a string.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You probably want l = int(input()) to convert the string from input into an integer.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
your code as shown is not going to work as you expect:
  • if (l==1):
    will only execute code below if l is equal to  1
  • The loop will always print the same thing, and that is the numbers 0 -  255
Reply
#7
thx for all the help :D

im here now
l = int(input("password lenght"))
if l ==1:
    for i in range(48,123):
        print(str(chr(i)))
    
if l ==2:
    for i in range(48,123):
        for j in range(48,123):
            print(chr(i)+chr(j))
its working and i can keep adding lenght but what if I want to skip those special characters and I just want numbers and letters?
characters i want to skip are 58,64 and 91,96
because if i use all 256 characters if the lenght is >3 it would take forever to run

i got it :)
Thx for the help
Reply
#8
Well, this will work too:

if l == '1':
    # actions
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020