如何修复在此基本Python程序中无法继续执行的代码?

时间:2019-07-12 02:28:23

标签: python python-3.x

几天前,我开始学习Python,并想创建一个“响应程序”,该程序做了一些基本的事情,例如显示日历或天气。一切正常,直到显示“我可以为您提供什么帮助?”。它只再次显示同一行。如果这是基础知识,很抱歉,我几天前才刚开始使用Python。

感谢您的帮助,

我已经尝试过移动类服务,但无法修复。另外,我尝试了PyCharm的建议,但无法正常工作。

export default function ResponsiveDrawer(props) {
  const { container, children } = props;
  const classes = useStyles();
  const theme = useTheme();
  const [mobileOpen, setMobileOpen] = React.useState(false);

  function handleDrawerToggle() {
    setMobileOpen(!mobileOpen);
  }

  const drawer = (
    <div>
        <div className={classes.toolbar} />
        <Divider />
        <MenuList>
            <Router>
                <MenuItem component={Link} to='/'> Dashboard </MenuItem>
                <MenuItem component={Link} to='/inventory'> Inventory </MenuItem>
                <MenuItem component={Link} to='/invoice'> Invoice </MenuItem>
                <MenuItem component={Link} to='/shipments'> Shipments </MenuItem>
                <MenuItem component={Link} to='/outbound'> Outbound </MenuItem>
                <MenuItem component={Link} to='/clinicalSamples'> Clinical Samples </MenuItem>
            </Router>
        </MenuList>
    </div>
  );

  return (
    <div className={classes.root}>
      <CssBaseline />
      <AppBar position="fixed" className={classes.appBar}>
        <Toolbar>
          <IconButton
            color="inherit"
            aria-label="Open drawer"
            edge="start"
            onClick={handleDrawerToggle}
            className={classes.menuButton}
          >
            <MenuIcon />
          </IconButton>
          <Typography variant="h6" noWrap>
            Inventory Management
          </Typography>
        </Toolbar>
      </AppBar>
      <nav className={classes.drawer} aria-label="Mailbox folders">
        {/* The implementation can be swapped with js to avoid SEO duplication of links. */}
        <Hidden smUp implementation="css">
          <Drawer
            container={container}
            variant="temporary"
            open={mobileOpen}
            onClose={handleDrawerToggle}
            classes={{
              paper: classes.drawerPaper,
            }}
            ModalProps={{
              keepMounted: true, // Better open performance on mobile.
            }}
          >
            {drawer}
          </Drawer>
        </Hidden>
        <Hidden xsDown implementation="css">
          <Drawer
            classes={{
              paper: classes.drawerPaper,
            }}
            variant="permanent"
            open
          >
            {drawer}
          </Drawer>
        </Hidden>
      </nav>
      <main className={classes.content}>
        <div className={classes.toolbar} />
            {children}
      </main>
    </div>
  );
}

I then call it in app.js like this:

render() {
    return (
    <React.Fragment>
     <div>
       <Router>
        <Drawer>
         <Switch>
           <Route path='/login' component={Login} exact/>
           <Route path='/' component={Home} exact />
           <Route path='/dashboard' component={Home} exact/>
           <Route path='/inventory' component={Inventory} exact />
           <Route path='/invoice' component={Invoice} exact />
           <Route path='/shipments' component={Shipments} exact />
           <Route path='/outbound' component={Outbound} exact />
           <Route component={Error} />
         </Switch>
        </Drawer>
       </Router>

     </div>
     </React.Fragment>

我不会仅收到代码无法继续的错误消息。

1 个答案:

答案 0 :(得分:1)

您有一个无限循环:

while True:
    action = input("I can do several things.I can check the [W]eather, or I can check the [C]alendar. What should I do?").upper()

# The rest of your code is outside the loop.
if action not in "WC" or len(action) != 1:
    print("I don't know how to do that")

获取用户输入并将其存储在action中之后,代码将重新启动while True循环。只有while循环之后缩进的代码才是循环的一部分。

您应该将代码移入循环。

while True:
    action = input("I can do several things.I can check the [W]eather, or I can check the [C]alendar. What should I do?").upper()

    if action not in "WC" or len(action) != 1:
        # do stuff

    elif action == 'W':
        # do stuff

    elif action == 'C':
        # do stuff

除了无限循环之外,您还需要解决以下其他问题:

  1. 了解如何正确并一致地缩进代码。缩进在Python中很重要。 PEP8 standard规定使用4个空格。您似乎使用了4个以上。

  2. 删除重复的不必要的class Services: pass代码。您已经具有使用class Services:weather属性定义的完整calendar。您无需将其嵌套在另一个Services类中。

    class Services:
        def __init__(self):
            self.weather
            self.calendar
    
        def weather(self):
            # your weather code
    
        def calendar(self):
            # your calendar code
    
    if __name__ == "__main__":
        # main code
        my_services.Services()
        # use my_services.weather...
        # use my_services.calendar...
    
  3. 在变量名中保持一致。您创建了my_service(单个)对象,但是在if-else块中使用了my_services(多个)。

最后,由于您提到使用PyCharm,因此学习如何使用Python调试器逐行浏览代码。调试器是检查代码问题的非常有用的工具。