0
import sys
users = {}


def createNewTable():
    id = 0
    while True:
        choise = int(
            input("What would you do with table?\n 1. Add new user\n 2. Read the table\n 3. Close the table\n"))
        match choise:
            case 1:
                name = str(input("Enter name of new user: "))
                users[id] = name
                id += 1
            case 2:
                print(users)
            case 3:
                nameOfFile = input(str("Enter name of file \n"))
                with ("{nameOfFile}.txt" , "w") as f:
                    f.write(str(users))
                print("File saving...")
                sys.exit()

На третьем кейсе после ввода переменной nameOfFile, компилятор выдает ошибку Вызвано исключение enter

2
  • Кажется, вы забыли написать open
    – andreymal
    17 авг 2022 в 12:39
  • Во-первых, для открытия файла нужно использовать функцию open. Сам по себе with ничего вам не откроет. Во-вторых, для подстановки переменной в строку в фигурных скобках нужно использовать f-строку (добавить f перед строкой: f"{nameOfFile}.txt")
    – insolor
    17 авг 2022 в 12:39

1 ответ 1

0

Кажется проблема лишь в том, что ты забыл указать после width слово open:

import sys
users = {}


def createNewTable():
    id = 0
    while True:
        choise = int(
            input("What would you do with table?\n 1. Add new user\n 2. Read the table\n 3. Close the table\n"))
        match choise:
            case 1:
                name = str(input("Enter name of new user: "))
                users[id] = name
                id += 1
            case 2:
                print(users)
            case 3:
                nameOfFile = input(str("Enter name of file \n"))
                with open("{nameOfFile}.txt" , "w") as f:
                    f.write(str(users))
                print("File saving...")
                sys.exit()
1
  • 1
    не только. Еще 1 проблему также написали в комментариях
    – qwerty
    17 авг 2022 в 12:55

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.