在for循环python 3中将def函数和打印放在一行上

时间:2017-04-17 23:10:07

标签: python python-3.x

我这里有一个线功能,这只是其中一个功能,部分是字典

#! /usr/bin/python3

from sleep import sleep
from read_lines import read_lines

import arduino
import sys
import time

variables = {}
commands = {}
contin = True
ci = 0
i = 0

def cmd_credits(parts):
    print('Written by Drew')

def cmd_connect(parts):
    arduino.connect()

def cmd_disconnect(parts):
    arduino.disconnect()

def cmd_date(parts):
    print(time.strftime("%Y-%m-%d"), end=' ')
    sys.stdout.flush()

def cmd_dec(parts):
    global variables
    value = parts[1]
    variables[value] = variables[value] -1

def cmd_inc(parts):
    global variables
    value = variables.get(parts[1])
    value += 1
    variables[parts[1]] = value

def cmd_input(parts):
    global variables
    if parts[1] == 'int':
        variables[parts[2]] = int(input())
    elif parts[1] == 'float':
        variables[parts[2]] = float(input())
    elif parts[1] == 'string':
        variables[parts[2]] = input()
    else:
        print('Unkonwn command')

def cmd_jump(parts):
    global lines
    global ci
    ci = ci + int(parts[1]) -1

def cmd_jumpeq(parts):
    global lines
    global variables
    global ci
    if variables.get(parts[1][1]) == int(parts[2]):
        ci = ci + int(parts[3]) -1

def cmd_jumpneq(parts):
    global lines
    global variables
    global ci
    if variables.get(parts[1][1]) != int(parts[2]):
        ci = ci + int(parts[3]) -1

def cmd_print(parts):
    global variables
    for i in range(1, len(parts)):
        if parts[i][0] == '$':
            print(variables[parts[i][1]])
        else:
            print(parts[i])

def cmd_println(parts):
    print(variables[parts])
    print(" ")

def cmd_receive(parts):
    global variables
    i = arduino.receive()
    variables['x'] = i

def cmd_sleep(parts):
    x = float(parts[1])
    if parts[2] == 's':
        time.sleep(x)
    elif parts[2] == 'm':
        x = x * 60
        time.sleep(x)
    elif parts[2] == 'h':
        x = x * 60 * 60
        time.sleep(x)
    elif parts[2] == 'ms':
        x = x/1000
        time.sleep(x)
    else:
        print('Invalid format')

def cmd_send(parts):
    sep = ' '
    lst = parts[1:]
    command_sent = sep.join(lst)
    arduino.send(command_sent)

def cmd_setint(parts):
    global variables
    if parts[2][0] == '$':
        variables[parts[1]] = variables[parts[2][1]]
    else:
        variables[parts[1]] = int(parts[2])

def cmd_time(parts):
    print(time.strftime("%I:%M:%S"), end=' ')
    sys.stdout.flush()

def process_cmdline_args():
    lines = []
    for fileName in sys.argv[1:]:
        file = open(fileName)
        while True:
            line = file.readline()
            line = line.strip()
            if not line:
                break
            lines.append(line)
        file.close()
    return lines

def cmd_unknown(parts):
    print('Unknown command:', parts)

def do_cmd(line):
    global commands
    cmdParts = line.split()
    while i in variables:
        if i[0] == '$':
            cmdParts = variables.append('i')
        else:
            cmdParts = variables
    cmdName = cmdParts[0]
    cmd = commands.get(cmdName, cmd_unknown)
    cmd(cmdParts)
#   print(cmd)

def initialize():
    global commands
    commands = {'input' : cmd_input, 'jumpneq' : cmd_jumpneq, 'jumpeq' : cmd_jumpeq, 'dec' : cmd_dec, 'inc' : cmd_inc, 'println' : cmd_println, 'receive' : cmd_receive, 'credits' : cmd_credits, 'date' : cmd_date, 'time' : cmd_time, 'sleep' : cmd_sleep, 'connect' : cmd_connect, 'disconnect' : cmd_disconnect, 'send' : cmd_send, 'jump' : cmd_jump, 'setint' : cmd_setint, 'print' : cmd_print}

def main():
    global lines
    global contin, ci
    initialize()
    lines = process_cmdline_args()
    if not lines:
        lines = read_lines()
    ci = 0
    contin = True
    while contin:
        if ci >= len(lines):
            contin = False
        else:
            line = lines[ci]
            ci = ci + 1
            do_cmd(line)
    print()

if __name__ == '__main__':
    main()
如果我要输入像

这样的命令,在启动该功能后

> print Please enter your name:
> input string name
> print Hi, $name

输出为

Please
enter
your
name:
Drew

如何在一行上打印?并使println功能正常工作?

0 个答案:

没有答案