Ich habe neulich angefangen mit dem Programmieren und habe mit Python als Anfängersprache begonnen.
Mein erstes Projekt, soll dem User entweder einen zufälligen Code aus entweder Buchstaben oder Zahlen generieren.
Dabei darf der User sich selbst aussuchen wie lange dieser zufällig generierte Code ist.
Mein Problem:
Der User soll an einer bestimmten Stelle im Code seine gewünschte Codelänge eingeben als Input.
Dieser wird dann durch einen Loop mit try geschickt um zu schauen, ob der input sich in ein int umwandeln lässt.
Danach versuche ich mit range die vom User eingegebene Zahl zu benutzen.
Leider klappt das nicht so ganz nach meinen Vorstellungen.
Der Code:
Code: Alles auswählen
#importing the needed moduls
import random
from random import randint
from random import choice
import string
from string import ascii_uppercase
#############################################################################
#create the main loop
right_input = 0
while right_input == 0:
#Welcome the user
print ("Welcome to the random word / number generator, made by Jax!")
print ("")
print ("Do you want to create a random word or a number?")
#initiating the first user input, that decides what the user wants to generate
user_choice = input("Please type in 'word' or 'number': ")
#initiating the first if clause, if the user has written the right strings
if user_choice == "word":
right_input = 1
print ("")
print ("You succesfully decided, to generate a random word")
print ("How many letters should your random word have?")
#initiating the loop for the right answer
right_wordlenght_input = 0
while right_wordlenght_input == 0:
#initiating the input, for the numbers of letters the random word should have
user_numbers_word_choice = input("Please type in a wished number for the lenght of the randomly generated word: ")
#initiating check if it is a number
try:
val = int(user_numbers_word_choice)
right_wordlenght_input = 1
print ("Your wished word lenght is:", user_numbers_word_choice)
print ("")
print ("Random word will be generatet. . .")
print(''.join(choice(ascii_uppercase) for i in range(user_numbers_word_choice)))
#adding the exception of user input aint a number
except:
print ("Thats not a number, please try again")
print ("")
elif user_choice == "number":
print ("")
print ("placeholder2")
right_input = 1
else:
print ("placeholder3")
print ("")Danke fürs Lesen, ich freue mich auf eure Antworten!
