-1

I am within my first week of python coding. well, first week of coding ever. I am trying to do a random name generator. However, I want to have it that if certain letters are chosen, another letter is added

I have lists for the consonants and vowels:

import random
vowels = ["a","e","i","o","u","y"]

consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","z"]

first = (consonants[random.randint(0,19)])

second = (vowels[random.randint(0,5)])

third = (consonants[random.randint(0,19)])

...

(however many letters desired. might do more randomness up to x letters)

However, I wanted:

if first == "q":
    first == "qu"

Let's say qezba was the name generated. I am trying to make conditions so that if a "q" is the first letter, it auto adds a "u" onto the string of the variable first. Or, if an "l" was the third letter, to add a "j" after it, half of the time

How could this be done?

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
  • single equals for assignment - `first = "qu"` – Sayse Jun 15 '22 at 05:28
  • 2
    Welcome to Stack Overflow. In your own words, where the code says `first == "qu"`, what do you expect that to do? What do you suppose is the meaning of `==`, and why do you suppose that Python expects you to use two `=` signs instead of just one? Have you seen Python code that uses just `=`? Do you understand what this does? – Karl Knechtel Jun 15 '22 at 05:29
  • 1
    He does say he is in his first week of coding. – doa4321 Jun 15 '22 at 05:34
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – Avi Jun 15 '22 at 10:02

2 Answers2

1

You can.

if first == "q":
  first = "qu"

Given your usecase, you can try:

if first == "q":
  first = first + "u"

== is the equality operator ( whether a equal to b ). = is the assignment operator.

doa4321
  • 176
  • 2
  • 9
  • I didn't downvote but its probably because this appears to be a typo given they know how to create variables. I also don't think the op is really wanting to check if `first` is `q` but they mention theres a "name" generated (not included in the question) and then depending on the first letter of that, append something to first. Not really sure what the op is asking. – Sayse Jun 15 '22 at 05:46
1

There are several ways to do it:

  1. if first=="q": first="qu"
  2. if first=="q": first+="u"
  3. first = (consonants[random.randint(0,19)])+"u"

Provided you are just starting out I suggest trying to solve the question yourself before asking here. Good luck

mir-shakir
  • 41
  • 5