繁体   English   中英

Python input() 函数在第一个输入上跳过一行

[英]Python input() function is skipping a line on the first input

我正在尝试进行文字冒险,但遇到了一个我无法解决的奇怪问题。 我不知道我改变了什么,因为我知道它在某些时候有效,但 startinput 不会接受第一个用户输入并跳到下一行所以例如我想写“开始”但出现的是:

"->

酸”

这是我导入主脚本的“引擎”文件

import os
import time
import random
import winsound
import sys

running = True

start_loop = True

#Time
epoch_seconds = time.time()
localtime = time.localtime(epoch_seconds)
localdate = str(localtime.tm_mday)+"."+str(localtime.tm_mon)+"."+str(localtime.tm_year)

#random number
r = 0

# 0 : Has not started yet
stage = 0

inventory = []

storyline = ""

hp = 100

rations = 20


#Input Template
unconditionalinputs = ["hilfe","menu","antwortmöglichkeiten","inventar","rationen"]

playerinput = ""


#Startup
def startup_sequence():
    while True:
        logo()
        load()
        print("---------------")
        print("START")
        print("---------------")
        print("CREDITS")
        print("---------------")
        print("HILFE")
        print("---------------")
        
        startinput = input("-> ")

        if startinput.lower() != "start" and startinput.lower() != "credits" and startinput.lower() != "hilfe":
            os.system("cls")
            print("Es gibt nur Start, Credits und Hilfe.")

        if startinput.lower() == "start":
            os.system("cls")
            time.sleep(1)
            break
            
        if startinput.lower() == "credits":
            os.system("cls")
            credits()

        if startinput.lower() == "hilfe":
            os.system("cls")
            help()
            input()
            os.system("cls")
    


def logo():
    print("---------------------------------------------------------------------------------------------------------\n                                                                                       ▄▄        \n▀████▀     █     ▀███▀                                ▀███▀▀▀███                  ██  ███        \n  ▀██     ▄██     ▄█                                    ██    ▀█                  ██   ██        \n   ██▄   ▄███▄   ▄█   ▄█▀██▄ ▀███▄███▀████████▄█████▄   ██   █   ▄█▀██▄ ▀███▄█████████ ███████▄  \n    ██▄  █▀ ██▄  █▀  ██   ██   ██▀ ▀▀  ██    ██    ██   ██████  ██   ██   ██▀ ▀▀  ██   ██    ██  \n    ▀██ █▀  ▀██ █▀    ▄█████   ██      ██    ██    ██   ██   █  ▄▄█████   ██      ██   ██    ██  \n     ▄██▄    ▄██▄    ██   ██   ██      ██    ██    ██   ██     ▄██   ██   ██      ██   ██    ██  \n      ██      ██     ▀████▀██▄████▄  ▄████  ████  ████▄██████████████▀██▄████▄    ▀███████  ████▄\n---------------------------------------------------------------------------------------------------------\n")


def credits():
    print("-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("Programmierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("----------------------------\nProgrammierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("Design/Storyline: Paul | Bajak | Rolo\n----------------------------\nProgrammierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("----------------------------\nDesign/Storyline: Paul | Bajak | Rolo\n----------------------------\nProgrammierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("Charakter Design: Paul | Bajak\n----------------------------\nDesign/Storyline: Paul | Bajak | Rolo\n----------------------------\nProgrammierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    os.system("cls")
    print("-----------------------------------\nCharakter Design: Paul | Bajak\n----------------------------\nDesign/Storyline: Paul | Bajak | Rolo\n----------------------------\nProgrammierung: Lynndog\n-----------------------------------")
    time.sleep(0.5)
    print("")
    input()
    os.system("cls")

def help():
    time.sleep(0.5)
    print_slow("-----------------------------------\n-Nach jeder Aktion (ausgenommen Frageunspezifische Eingabemöglichkeiten) wird eine Ration verbraucht\n\n-Die Fragespezifischen Eingabemöglichkeiten können mit -Antwortmöglichkeiten abgerufen werden\n-Wenn das Spiel stoppt kann man meißtens mit Enter weiter\n-Andere Eingabemöglichkeiten sind:\n   -Inventar (um sein Inventar anzuzeigen)\n   -Rationen (zeigt die verbleibenden Rationen)\n   -Hilfe (um dieses Menü aufzurufen)\n   -Menu (Zurück ins Menü, ACHTUNG SPEICHERT NICHT NOCHMAL EXTRA)\n-----------------------------------",0.001)
    print("")


#Add to Inventory if not already there
def add_to_inventory(item):
    if item not in inventory:
        inventory.append(item)
    else:
        inventory
        print_slow("->Du hast bereits: "+str(item),0.1)  

def show_inventory():
    print(*inventory, sep=", ")

def clear_inventory():
    inventory=[]

#Add or remove rations
def add_rations(added_rations):
    global rations
    rations += added_rations
    print_slow("->Du hast "+str(added_rations)+" Ration/en erhalten!",0.1)

def remove_rations(removed_rations):
    global rations
    rations -= removed_rations
    print_slow("->Du hast "+str(removed_rations)+" Ration/en verloren!",0.1)

#Add or remove hp
def add_hp(added_hp):
    global hp
    if hp + added_hp <= 100:
        hp+=added_hp
    else:
        hp=100

def remove_hp(removed_hp):
    global hp
    if hp - removed_hp <= 0:
        die()
    else:
        hp-=removed_hp


#Die
def die():
    print("                            ▄▄         ▄▄                                                                  ▄▄                           \n▀███▀▀▀██▄                 ▄██         ██          ██                               ██                    ▄██                           \n  ██    ▀██▄                ██                     ██                               ██                     ██                           \n  ██     ▀█████  ▀███       ██▄████▄ ▀███  ▄██▀████████     ▄█▀█████ ▄▄█▀██ ▄██▀████████  ▄██▀██▄▀███▄███  ██▄████▄   ▄▄█▀██▀████████▄  \n  ██      ██ ██    ██       ██    ▀██  ██  ██   ▀▀ ██      ▄██  ██  ▄█▀   ████   ▀▀ ██   ██▀   ▀██ ██▀ ▀▀  ██    ▀██ ▄█▀   ██ ██    ██  \n  ██     ▄██ ██    ██       ██     ██  ██  ▀█████▄ ██      ▀█████▀  ██▀▀▀▀▀▀▀█████▄ ██   ██     ██ ██      ██     ██ ██▀▀▀▀▀▀ ██    ██  \n  ██    ▄██▀ ██    ██       ██▄   ▄██  ██  █▄   ██ ██      ██       ██▄    ▄█▄   ██ ██   ██▄   ▄██ ██      ██▄   ▄██ ██▄    ▄ ██    ██  \n▄████████▀   ▀████▀███▄     █▀█████▀ ▄████▄██████▀ ▀████    ███████  ▀█████▀██████▀ ▀████ ▀█████▀▄████▄    █▀█████▀   ▀█████▀████  ████▄\n                                                           █▀     ██                                                                                                                               ██████▀                                                                      \n")
    input()
    startup_sequence()


#Add to storyline
def add_to_storyline(storyline_text):
    global localdate
    global storyline
    storyline = storyline+"\n"+(str(localdate)+"\n"+storyline_text)


#Sounds
def play_sound(soundfile):
    try:
        winsound.play_sound("sounds/"+soundfile)
    except:
        pass


def print_slow(str,timebetweenchars):
    for letter in str:
        print(letter, end ='', flush = True)
        time.sleep(timebetweenchars)
    print("")


#Check for the not Question-specific inputs
def inputcheck(inputmessage,otherinputslist2):
    inputmessage = inputmessage.lower()
    if inputmessage == "hilfe":
        help()
    if inputmessage == "menu":
        running = False
        os.system("cls")
    if inputmessage == "antwortmöglichkeiten":
        print(*otherinputslist2, sep=", ")
    if inputmessage == "inventar":
        show_inventory()
    if inputmessage == "rationen":
        print_slow("Du hast noch "+str(rations)+" Rationen",0.1)

def inputtemplate(otherinputslist):
    global playerinput
    global rations

    playerinput = ""
    while playerinput not in otherinputslist:
        playerinput = input(": ")
        inputcheck(playerinput,otherinputslist)
        while playerinput.lower() not in unconditionalinputs and playerinput.lower() not in otherinputslist:
            print_slow("Diese Eingabemöglichkeit gibt es nicht!",0.1)
            playerinput = input(": ")
            inputcheck(playerinput,otherinputslist)
    if playerinput in otherinputslist:
        if rations == 0:
            print_slow("Du hast keine Rationen mehr. -1HP",0.1)
            print_slow("-> Du hast noch "+ str(hp)+"HP",0.1)
        if rations > 0:
            rations -= 1
    

#Saving and Loading
def make_save_dirs():
    if not os.path.exists("save_files"):
        os.makedirs("save_files")

    if not os.path.exists("sounds"):
        print_slow("Sound Files Missing!",0.1)

    if not os.path.exists("save_files/stage.txt"):
        f = open("save_files/stage.txt", "w")
        f.write(str(stage))
        f.close()
    
    if not os.path.exists("save_files/inventory.txt"):
        f = open("save_files/inventory.txt", "w")
        f.close()

    if not os.path.exists("save_files/storyline.txt"):
        f = open("save_files/storyline.txt", "w")
        f.close()

    if not os.path.exists("save_files/hp.txt"):
        f = open("save_files/hp.txt", "w")
        f.write(str(hp))
        f.close()

    if not os.path.exists("save_files/rations.txt"):
        f = open("save_files/rations.txt", "w")
        f.write(str(rations))
        f.close()


#Saving and Loadign the Inventory
def save_inventory():
    f = open("save_files/inventory.txt", "w+")
    for item in inventory:
        f.write("%s\n" % item)
    f.close()

def load_inventory():
    f = open("save_files/inventory.txt", "r")
    for line in f:
        x = line[:-1]
        inventory.append(x)
    f.close()


#Saving Any Int, Str etc.
def save_anything(save_file_name, variable):
    f = open("save_files/"+save_file_name, "w+")
    f.write(str(variable))
    f.close

#Load Int
def load_int(save_file_name):
    f = open("save_files/"+save_file_name, "r")
    variable = int(f.read())
    return variable
    f.close()

#Load Str
def load_str(save_file_name):
    f = open("save_files/"+save_file_name, "r")
    variable = f.read()
    return variable
    f.close()


def save():
    make_save_dirs()
    save_inventory()
    save_anything("stage.txt", stage)
    save_anything("storyline.txt", storyline)
    save_anything("hp.txt", hp)
    save_anything("rations.txt", rations)

    print("---------------")
    print_slow("--> Spiel wurde gespeichert",0.1)
    print("---------------")

def load():
    global stage
    global storyline
    global hp
    global rations
    make_save_dirs()
    load_inventory()
    stage = load_int("stage.txt")
    hp = load_int("hp.txt")
    storyline = load_str("storyline.txt")
    rations = load_int("rations.txt")

这是主要脚本

import quest_engine as qe
from quest_engine import *


#Time
epoch_seconds = time.time()
localtime = time.localtime(epoch_seconds)
localdate = str(localtime.tm_mday)+"."+str(localtime.tm_mon)+"."+str(localtime.tm_year)
year = localtime.tm_year
game_year = year+100



while qe.running:
    os.system("cls")

    startup_sequence()

    # 0 TUTORIAL
    # 1 BRENNENDES AUTO
    # 2 IN DER SCHULE

    if qe.stage == 0:
        add_to_storyline("Tagebuch gefunden.")
        print_slow("02.10."+str(game_year)+"\nIch habe heute aus dem Fenster geschaut.\nIch denke oft über die Zukunft nach in letzter Zeit.\nSie macht mir Sorgen,\nSie macht mir Angst.\n",0.01)
        input()
        print_slow("15.03."+str(game_year+16)+"\nEs ist soweit.\nZum Glück konnte ich noch mein Tagebuch greifen.\nEs gibt keinen Strom mehr, nurnoch wenig Nahrung.\nJeder Tropfen Benzin ist leer.\n",0.01)
        input()
        print_slow("21.10."+str(game_year+33)+"\nMein Gott ich habe die Lösung gefunden!\nDamit können wir den Planeten kühlen!\nIch bringe diesen Plan zur alten Schule.\nDort werden wir ihn in die Tat umsetzen!\n",0.01)
        input()
        print_slow("Du: Oh schau mal!\n",0.01)
        input()
        print_slow("?: Was ist das?\n",0.01)
        input()
        print_slow("?: Oh Interessant.\n",0.01)
        input()
        print_slow("Du: Schule..\n",0.01)
        input()
        print_slow("Du: Kennst du eine Schule hier in der nähe?\n",0.01)
        input()
        print_slow("?: Naja im Norden ist glaub ich eine. Aber ich bin mir nicht sicher.\n",0.01)
        input()
        print_slow("Du: Wer weiß, aber wenn wir Glück haben hat der Typ vom Tagebuch was dort gelassen.\n",0.01)
        input()
        print_slow("?: Ja lass uns hingehen!\n",0.01)
        add_to_storyline("Auf den Weg zur Schule.")
        add_to_inventory("Tagebuch")
        input()
        os.system("cls")
        qe.stage = 1
        save()

    if qe.stage == 1:
        print("---------------")
        print_slow("TEIL 1: Auf der suche nach dem Wissenschaftler",0.01)
        print("\n---------------")
        print_slow("Du triffst auf ein brennendes Auto.",0.01)
        print_slow("\n                            @@@@@@@@@@@@@@%                                     \n                    .@@            @@@         @@@                              \n                @@@                 @@               @@                         \n              @@                    @@                  @@@                     \n          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#       \n  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  \n @@@@@@@@@@@@@@ @@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@ (@@@@@@@@ \n.@@@@@@@@@@@@ @@@      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@      /@@@ @@@@@@@\n @@@@@@@@@@@ @@          @@&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@         @@@/@@@@@@\n         (@@ @@          @@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@         @@@@@@@@@ \n              @@@      &@@@                                @@@       @@@        \n                @@@@@@@@                                     @@@@@@@@@          \n                                                                                \n",0.01)
        input()
        print_slow("Du hörst schreihe aus dem Auto hervorkommen!",0.01)
        input()
        print_slow("Möchtest du dem Gefangenen helfen?",0.01)
        
        inputtemplate(["ja","nein"])
        if qe.playerinput == "ja":
            qe.r = random.randint(1,100)
            #Branndwunde
            if qe.r <= 33:
                qe.hp -= random.randint(5,8)
                add_to_storyline("Du bekommst Schaden beim retten einer Person in einem brennenden Auto. Du hast noch"+ str(qe.hp)+"HP.")
                print_slow("Leider bekommst du Schaden beim retten der Person und hast nur noch "+str(qe.hp)+"HP",0.01)
                input()


            #Keine Wunden
            if qe.r > 33:
                add_to_storyline("Du rettest eine Person aus einem brennenden Auto.")
                print_slow("Du hast Glück und bekommst keinen Schaden beim retten der Person.",0.01)
                input()

            print_slow("Person: Oh Gott.. *hust*",0.01)
            input()
            print_slow("Person: Wie kann ich dir danken? *hust*",0.01)
            input()
            if qe.r > 33:
                print_slow("Person: Zum Glück bist du unversehrt! Hier.. nimm ein Paar meiner Rationen zum Dank!",0.01)
                add_rations((random.randint(2,4)))

            if qe.r <= 33:
                print_slow("Person: Oje. Es tut mir leid das ich dich da reingezogen habe. Hier.. nimm ein Paar meiner Rationen zum Dank!",0.01)
                add_rations((random.randint(4,7)))
            input()
            print_slow("Du bedankst dich bei ihm und gehst weiter richtung Schule.",0.01)

        if qe.playerinput == "nein":
            print_slow("Du ignorierst die schrecklichen Schreihe und gehst entschlossen weiter!",0.01)
            
        input()
        qe.stage = 2
        save()

    if qe.stage == 2:
        print_slow("?: Hey schau mal!",0.01)
        input()
        print_slow("?: Dort hinten ist die Schule.",0.01)
        input()
        print_slow("Ihr nähert euch der Eingangstür.",0.01)
        input()
        print_slow("Du: Okay. Bevor wir dort jetzt reingehen, will ich noch wissen wer du bist. Wer weiß was da drinn passiert",0.01)
        input()
        print_slow("?: Nagut ich sage es dir",0.01)

我不知道它是否重要,但我通过这样的批处理文件启动主文件:

@echo off
start /b "WarmEarth" python "warmearth.py"
pause

它工作正常(本地测试)

def logo():
    print("---------------------------------------------------------------------------------------------------------\n                                                                                       ▄▄        \n▀████▀     █     ▀███▀                                ▀███▀▀▀███                  ██  ███        \n  ▀██     ▄██     ▄█                                    ██    ▀█                  ██   ██        \n   ██▄   ▄███▄   ▄█   ▄█▀██▄ ▀███▄███▀████████▄█████▄   ██   █   ▄█▀██▄ ▀███▄█████████ ███████▄  \n    ██▄  █▀ ██▄  █▀  ██   ██   ██▀ ▀▀  ██    ██    ██   ██████  ██   ██   ██▀ ▀▀  ██   ██    ██  \n    ▀██ █▀  ▀██ █▀    ▄█████   ██      ██    ██    ██   ██   █  ▄▄█████   ██      ██   ██    ██  \n     ▄██▄    ▄██▄    ██   ██   ██      ██    ██    ██   ██     ▄██   ██   ██      ██   ██    ██  \n      ██      ██     ▀████▀██▄████▄  ▄████  ████  ████▄██████████████▀██▄████▄    ▀███████  ████▄\n---------------------------------------------------------------------------------------------------------\n")

def startup_sequence():
    while True:
        logo()
        # load()
        print("---------------")
        print("START")
        print("---------------")
        print("CREDITS")
        print("---------------")
        print("HELP")
        print("---------------")
        
        startinput = input("-> ")
        print(startinput)

startup_sequence()

唯一会搞砸的是加载函数,问题描述中没有包含

实际上这是一个 cmd 错误。 当我打开没有批处理文件但通过控制台打开程序时,它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM