Python Forum
Troubles with program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with program
#11
Good news! I did something like this:
import random
import string
number=random.randrange(1500)+1
letter=random.choice(['a','b','c','d','e','f','g','h'])
dot='.'
for i in range(100):
    number=random.randrange(1500)+1
    letter1=random.choice(['a','b','c','d','e','f','g','h'])
    letter2=random.choice(['a','b','c','d','e','f','g','h'])
    letter3=random.choice(['a','b','c','d','e','f','g','h'])
    letter4=random.choice(['a','b','c','d','e','f','g','h'])
    print(number,dot,letter1,letter2,letter3,letter4)

It isnt useless to make a dot after the number (240., 321., etc.) It always make with a space but it is just a cosmetic error. Doesnt care.
Now I expect to use if or something like this to determinate the numbers and letters not to repeat. I need to think about how to do it.
Reply
#12
You can also use a while loop like :
import random
count = 1
while count <= 100 :
    num1 = random.randint(1,1501)
    str1 = random.choice(['a','b','c','d','e','f','g','h'])
    str2 = random.choice(['a','b','c','d','e','f','g','h'])
    str3 = random.choice(['a','b','c','d','e','f','g','h'])
    str4 = random.choice(['a','b','c','d','e','f','g','h'])
    final_ans = str(num1) + str1 + str2 + str3 + str4
    print(f"Number {count} : {final_ans}")
    count += 1
And output :
Output:
Number 1 : 379fegf Number 2 : 1092hhhg Number 3 : 1270gfha .....

(May-28-2020, 10:04 AM)Harambe Wrote: Now I expect to use if or something like this to determinate the numbers and letters not to repeat. I need to think about how to do it.
Do you mean like the number or letter shouldn't be repeated in 1 given instance?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#13
Thank you for advice, but I need output like this:
Output:
21. a,f,g,h 45. b,c,e,g 77. a,d,f,h 394. b,c,d,g ...
It would be nice if there is any poissibility to sort the numbers and letters in the order (1, 2, 3, 4 and a, b, c, d, etc...)
Btw, what is
 print(f"Number {count} : {final_ans}") 
“f” here for?
Reply
#14
(May-28-2020, 10:25 AM)Harambe Wrote: Thank you for advice, but I need output like this:
Output:
21. a,f,g,h 45. b,c,e,gis 77. a,d,f,h 394. b,c,d,g ...
Sure, no problem - I just wanted to show you another way of doing so
(May-28-2020, 10:25 AM)Harambe Wrote: Btw, what is
 print(f"Number {count} : {final_ans}") 
“f” here for?
The f in the given line is a formatted string - to know more see this or this (I'd recommend this one)
Formatted strings can also be written as
print("Number %s : %s" % (count,final_ans))
or
print("Number {} : {}" .format(count,final_ans))
However, the most preferred one would be the one i posted in the code the f-string formatting - they came into use in Python 3.6 and are one of its greater advantages
(May-28-2020, 10:25 AM)Harambe Wrote: It would be nice if there is any poissibility to sort the numbers and letters in the order (1, 2, 3, 4 and a, b, c, d, etc...)
Do you mean like in ascending order?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#15
Yes, in ascending order. Meanwhile I was thinking about this:
import random
number=random.randrange(1500)+1
letter=random.choice(['a','b','c','d','e','f','g','h'])
dot='.'
for i in range(100):
    number=random.randrange(1500)+1
    letter1=random.choice(['a','b','c','d','e','f','g','h'])
    letter2=random.choice(['a','b','c','d','e','f','g','h'])
    letter3=random.choice(['a','b','c','d','e','f','g','h'])
    letter4=random.choice(['a','b','c','d','e','f','g','h'])
    if letter1='a':
        print(letter2=random.choice(['b','c','d','e','f','g','h'])
    elif letter1='b':
        print(letter2=random.choice(['a','c','d','e','f','g','h'])
    else letter1='c':
        print(letter2=random.choice(['a','b','d','e','f','g','h'])
I want to determinate all 4 letters like this to avoid repeating. But I cant find why the program is not working now.
Reply
#16
= is used to define whereas == is used to compare - change it
and also your print statement is wrong
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#17
import random
number=random.randrange(1500)+1
letter=random.choice(['a','b','c','d','e','f','g','h'])
dot='.'
for i in range(100):
    number=random.randrange(1500)+1
    letter1=random.choice(['a','b','c','d','e','f','g','h'])
    letter2=random.choice(['a','b','c','d','e','f','g','h'])
    letter3=random.choice(['a','b','c','d','e','f','g','h'])
    letter4=random.choice(['a','b','c','d','e','f','g','h'])
    if letter1=='a':
        print(number,letter2=random.choice(['b','c','d','e','f','g','h']),letter3,letter4)
    elif letter1=='b':
        print(number,letter2=random.choice(['a','c','d','e','f','g','h']),letter3,letter4)
    else letter1=='c':
        print(number,letter2=random.choice(['a','b','d','e','f','g','h']),letter3,letter4)
I think the command of letter 2 determination shouldnt be in the print command but I cant make the structure of it.
Reply
#18
You can use random.sample() to avoid repetition.

letters = random.sample(['a','b','c','d','e','f','g','h'], 4)
This code will randomly select 4 values from the list of letters without any repeated values.
Reply
#19
  • You can't use
    else letter1 == "c":
    - else statements are written alone - so make it elif
  • In lines 12, 14 and 16 your print statement is wrong- change it to
    letter2 = random.choice(['b','c','d','e','f','g','h'])
    print(number,letter2,letter3,letter4)
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#20
Almost got it! This command helped me a lot! I am wondering I didnt find it on the internet.
import random
number=random.sample(range(1500),1)
letters=random.sample(['a','b','c','d','e','f','g','h'],4)
for i in range(100):
    number=random.sample(range(1500),1)
    letters=random.sample(['a','b','c','d','e','f','g','h'],4)
    print(number,letters)
My program is almost done. Now the last problem. How to sort the numbers and letters? And I am not sure about my number command. I think it will still give me numbers with possible repetition.

Btw. the output is like this:
Output:
([82], ['g', 'h', 'e', 'a']) ([440], ['b', 'c', 'a', 'g']) ([1157], ['f', 'h', 'g', 'c']) ([465], ['g', 'h', 'c', 'a']) ([1349], ['g', 'f', 'b', 'c']) ([635], ['b', 'e', 'a', 'h']) ([148], ['g', 'e', 'h', 'a']) ([1456], ['d', 'b', 'h', 'c']) ([204], ['e', 'h', 'a', 'b']) ([1346], ['b', 'g', 'c', 'a']) ([703], ['g', 'a', 'f', 'd']) ([924], ['b', 'h', 'd', 'g']) ([1314], ['f', 'h', 'a', 'd']) ([887], ['c', 'a', 'b', 'h']) ([484], ['f', 'c', 'b', 'a']) ([1483], ['e', 'd', 'h', 'b'])

What is wrong?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a list troubles giveen 7 3,979 Jan-11-2019, 08:05 PM
Last Post: giveen
  Troubles on how to use Open CV knowledge1st 1 2,504 May-23-2018, 05:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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