简体   繁体   中英

How do I separate the username input from the game input?

I wrote a game called Rock Rock Rock, which is a stupid variation of Rock Paper Scissors where the only option is Rock (but there are some special things I added in too) and I've also added a Username just for the fun of it. I noticed while testing the game that anytime I enter "sharp" or "or" as a username, the game simply closes. I've been trying to find a way to separate them, but google has no answers and I'm not smart enough to figure it out. Can someone help me? Thanks: Here's my code:

import time
import random
import os
import sys

message = ""
wait = 0
wins = 0
ties = 0
ores = 0
SR = 0
CR = 0
allow_input = False

def wait(wait):
    time.sleep(wait)

def typeprint(message):
    for char in message:
        sys.stdout.write(char)
        sys.stdout.flush()
        wait(0.03)

typeprint("Enter your username: ")
user = input()
typeprint("\nWelcome, " + user())
typeprint("\nType HELP for commands")
typeprint("\n\nLoading...\n\n")

running = True
while running:
    allow_input = True
    wait(1)
    typeprint("Choose Rock, Rock, or Rock: ")
    x = raw_input()
    typeprint("\nYou chose: " + x())

    if x.upper() == "ROCK":
        wait(1)
        typeprint("\nOpponent chose: Rock")
        wait(1)
        typeprint("\nIt's a tie!\n\n")
        ties = ties + 1
        typeprint("Ties: " + str(ties))

    elif x.upper() == "SHARP ROCK":
        wait(1)
        typeprint("\nOpponent chose: Rock")
        wait(1)
        if SR == 0:
            typeprint("\nhmmm...")
            wait(2)
            typeprint("\nWell...it's not a tie...so I guess you win anyway")
            wins += 1
            typeprint("Wins: " + str(wins))
        else:
            typeprint("Fine, you get another one...")
            typeprint("Wins: " + str(wins))

    elif x.upper() == "HELP":
        wait(1)
        typeprint("\nCommands: ")
        typeprint("\n1) Rock: Chooses 'Rock', 'Rock', or 'Rock'")
        typeprint("\n2) Wins: Displays Wins and Ties")
        typeprint("Try mixing it up a little :)" + " \n\n")

    elif x.upper() == "CRUMPLED PAPER":
        CR += 1
        wait(1)
        print("\nInterface eth-0-51")
        wait(0.05)
        print("Interface current state: UP Hardware is WIFI")
        wait(0.05)
        print("address is 649d.992f.e91c (bia 649d.992f.e91c)")
        wait(0.05)
        print("Description: Bandwidth 40000000 kbits Index 51")
        wait(0.05)
        print("Metric 1\nEncapsulation ARPA Speed - 40Gb/s")
        wait(0.05)
        print("Duplex - Full\nMedia type is 40GBASE_SR4")
        wait(0.05)
        print("Last up time(UTC): 2022-08-27 03:11:30 Last down time: -")
        wait(0.05)
        print("Current system time(UTC): 2022-09-18 05:27:13")
        wait(0.05)
        print("Link type is force link FEC config: DISABLE")
        wait(0.05)
        print("FEC status: DISABLE Admin input flow-control is off")
        wait(0.05)
        print("output flow-control is off")
        wait(0.05)
        print("Oper input flow-control is off")
        wait(0.05)
        print("output flow-control is off")
        wait(0.05)
        print("The Maximum Frame Size is 9600 bytes VRF binding: not bound ARP timeout 01:00:00")
        wait(0.05)
        print("ARP retry interval 1s ARP Proxy is disabled")
        wait(0.05)
        print("Local ARP Proxy is disabled 5 minute \ninput rate 12792167 bits/sec")
        wait(0.05)
        print("3161 packets/sec \n5 minute output rate")
        wait(0.05)
        print("5224387 bits/sec")
        wait(0.05)
        print("1893 packets/sec")
        wait(0.05)
        print("39665739577 packets input")
        wait(0.05)
        print("55751139253257 bytes Received")
        wait(0.05)
        print("39288853436 unicast")
        wait(0.05)
        print("338196487 broadcast")
        wait(0.05)
        print("38689654 multicas")
        wait(0.05)
        print("0 runts")
        wait(0.05)
        print("34798777985 giants")
        wait(0.05)
        print("0 input errors")
        wait(0.05)
        print("0 CRC")
        wait(0.05)
        print("0 frame")
        wait(0.05)
        print("0 overrun")
        wait(0.05)
        print("0 wait input")
        wait(0.05)
        print("13347013857 packets output")
        wait(0.05)
        print("14739413116064 bytesTransmitted")
        wait(0.05)
        print("13346859597 unicast")
        wait(0.05)
        print("26366 broadcast")
        wait(0.05)
        print("127894 multicast")
        wait(0.05)
        print("0 underruns")
        wait(0.05)
        print("0 output errors")
        wait(0.05)
        print("0 wait output")
        wait(0.05)
        print("0 output discard")
        wait(1)
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\nyou win!\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        wins = wins + 1
        wait(1)
        print("ERROR: 455938727; invalid sequence bypassed; " + "user " + user() + " has cheated; kicking player...\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        wait(4)
        print("Restarting...\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        wait(2)
        typeprint("Well, ya broke it...so it gave you a win anyway. Good job for figuring this out!")
        typeprint("Wins: " + str(wins))
    elif x.upper() == "END":
        wait(1)
        typeprint("\n STOPPING PROGRAM...")
        wait(3)
        os.system('cls')
        break

    elif x.upper() == "OR":
        wait(1)
        typeprint("\nOpponent chose: Rock")
        wait(1)
        ores += 1
        wait(1)
        if ores == 1:
            typeprint("\nI like your style!")
            typeprint("\nI'll add in a style points section for that :)")
            wait(1)
        if ores == 2:
            typeprint("\nOkay! You get another one!")
            typeprint("\nStyle points: " + str(ores) + "\n\n")
        else:
            typeprint("\nStyle points: " + str(ores) + "\n\n")
    elif x.upper() == "WINS":
        wait(1)
        typeprint("\nYou have won: " + str(wins) + " times")
        typeprint("\nYou have tied: " + str(ties) + " times\n\n")
        typeprint("\nYou have: " + str(ores) + " style points\n\n")
        wait(1)

    if SR == 1 and CR == 1:
        typeprint("\nYou are quite the creative one, aren't you...I love it! :)\n")

    else:
        wait(1)
        typeprint("\nInvalid command, try again\n\n")
        wait(1)
        os.system('cls')

The problem you're facing lies on line 26. You should be calling user instead of user() since it's a variable not a method. Same thing on line 36, you'll need to call x instead of x() .

On line 35, you should be using x = input() instead of x = raw_input() . raw_input() was a python2 function which is now just input() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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