Python Wort nur bis zum 1. Vokal ausgeben?


20.05.2023, 19:06

@Computihack:

Tut mir leid, der Platz reicht nicht - es sind nur höchstens 3000 Zeichen in der gesamten Frage erlaubt... Sorry!

Computihack  20.05.2023, 18:05

Kannst du bitte den code als text hochladen?

CoolCreeper400 
Fragesteller
 20.05.2023, 19:04

ja mach ich, gerne!

3 Antworten

def vstrip(s):
    v="aeiou"
    return s[:min(s.index(vowel) for vowel in v if vowel in s)]

>>> vstrip("Wurstsuppe")
'W'
>>> vstrip("Bratpfanne")
'Br'
>>> vstrip("Physikklaususr")
'Phys'

So in etwa?

Bequemer ginge es mit einem RegExp, exemplatrisch, z.B.

import re

>>> re.split("[aeiou]+","Wurstsuppe",flags=re.I)
['W', 'rsts', 'pp', '']

Für den Fall, daß ein Wort mit einem Vokal beginnt, wäre das erste Element dann ein leerer String. Lässt Du das Ignore case weg, wäre es bis zum ersten kleienn Vokal, Beispielhaft:

>>> re.split("[aeiou]+","Apfelkuchen",flags=re.I)
['', 'pf', 'lk', 'ch', 'n']
>>> re.split("[aeiou]+","Apfelkuchen")
['Apf', 'lk', 'ch', 'n']

Es gibt noch mehr Optionen, wie man das mit RexExps lösen kann, das sollte nur eien weitere Möglichkeit zeigen.

Ai kann helfen

import random
import string
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def generate_password():
    """
    Generates a personalized and secure password using user input.

    :return: Personalized and secure password.
    :rtype: str
    """
    
    # Define character pools for password generation
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    special_chars = ["/", "(", ")", "{", "}", "[", "]", "&", "%", "!", ",", ".", "?"]
    
    # Welcome message and instructions
    print("Welcome to the password generator!")
    print("Please provide four personal words to be included in your password.")
    print("Avoid using typical words like names or birth dates as they can be easily guessed.")
    
    # Get user input for personal words
    word1 = input("First word: ")
    word2 = input("Second word: ")
    word3 = input("Third word: ")
    word4 = input("Fourth word: ")
    
    # Find first vowel in each word and truncate words accordingly
    def find_first_vowel(word):
        for vowel in vowels:
            if vowel in word:
                return word.index(vowel)
        return -1
    
    first_vowel1 = find_first_vowel(word1)
    first_vowel2 = find_first_vowel(word2)
    first_vowel3 = find_first_vowel(word3)
    first_vowel4 = find_first_vowel(word4)
    
    word1 = word1[:first_vowel1] if first_vowel1 != -1 else word1
    word2 = word2[:first_vowel2] if first_vowel2 != -1 else word2
    word3 = word3[:first_vowel3] if first_vowel3 != -1 else word3
    word4 = word4[:first_vowel4] if first_vowel4 != -1 else word4
    
    # Get user input for favorite number
    favorite_number = int(input("What is your favorite number? "))
    
    # Get user input for special character
    special_char = input("If you want to use a specific special character, enter it now. Otherwise, type 'no': ")
    if special_char == "no":
        special_char = random.choice(special_chars)
        logger.info("Random special character selected: %s", special_char)
    else:
        logger.info("User selected special character: %s", special_char)
    
    # Generate second random special character
    second_special_char = random.choice(special_chars)
    logger.info("Second random special character selected: %s", second_special_char)
    
    # Generate two random numbers
    end_number1 = random.choice(numbers)
    end_number2 = random.choice(numbers)
    logger.info("Random numbers selected: %s, %s", end_number1, end_number2)
    
    # Combine all parts to form password
    password = f"{word1}{word2}{favorite_number}{word3}{special_char}{word4}{second_special_char}{end_number1}{end_number2}"
    logger.info("Password generated: %s", password)
    
    # Get user feedback and rating
    feedback = int(input("Do you like your password? If not, you can generate a new one. Please rate the password generator on a scale of 1 to 10: "))
    if feedback < 4:
        print("We're sorry to hear that.")
    elif feedback >= 4 and feedback < 7:
        print("We appreciate your feedback and will work to improve the password generator.")
    elif feedback >= 7 and feedback < 10:
        print("Thank you for your feedback! We will continue to improve the password generator.")
    else:
        print("Thank you for your feedback! We're glad you like the password generator.")
    
    return password

edit:

bessere version:

import random
import logging


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


vowels = "aeiouAEIOU"
numbers = "0123456789"
special_chars = "/(){}[]&%,.?!"


print("Welcome to the password generator!")
print("Please provide four personal words to be included in your password.")
print("Avoid using typical words like names or birth dates as they can be easily guessed.")


words = [input(f"{i + 1}. word: ") for i in range(4)]


truncated_words = [word[:next((index for index, char in enumerate(word) if char in vowels), len(word))] for word in words]


favorite_number = int(input("What is your favorite number? "))


special_char = random.choice(special_chars) if input("If you want to use a specific special character, enter it now. Otherwise, type 'no': ") == "no" else special_char
logger.info("User selected special character: %s", special_char)


password_parts = [''.join(truncated_words), str(favorite_number), special_char, random.choice(special_chars), random.choice(numbers), random.choice(numbers)]
password = ''.join(password_parts)
logger.info("Password generated: %s", password)


feedback = int(input("Do you like your password? If not, you can generate a new one. Please rate the password generator on a scale of 1 to 10: "))
feedback_message = ["We're sorry to hear that.", "We appreciate your feedback and will work to improve the password generator.", "Thank you for your feedback! We will continue to improve the password generator.", "Thank you for your feedback! We're glad you like the password generator."][feedback < 4] if feedback < 7 else 2 if feedback < 10 else 3
print(feedback_message)


Woher ich das weiß:Studium / Ausbildung – Information Engineering Studium

In deiner Funktion wannVokal müssen wir mal aufräumen...

1) dir 4 globals für wert1-4 brauchst du nicht. Du übergibtst ja deine worte beim Funktionsaufruf

2) musst du die Position die du in der funktion ermittelst auch zurückgeben damit du die beim aufrufen in eine variable zurück schreiben kannst. Momentan gibst du nichts zurück sodass die entsprechenden variablen leer sind.

Woher ich das weiß:Hobby